2016-07-30 55 views
2

在python,selenium,W​​in 7下工作,我想点击sowcomment按钮后就可以点击位于这个webPage,由wait处理,然后我想点击showmore Comments按钮来查看更多评论以便刮掉更多评论。第一个按钮后,我可以提取评论。如何使用硒python3单击一个网页上的第二个按钮?

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
import selenium.common.exceptions 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
import selenium.webdriver.support.ui as UI 

driver = webdriver.Firefox() 
driver.get("http://www.aljazeera.com/news/2016/07/erdogan-west-mind-business-160729205425215.html") 
wait = UI.WebDriverWait(driver, 10) 
next_page_link = wait.until(
       EC.element_to_be_clickable((By.ID, 'showcomment'))) 
next_page_link.click() 

wait = UI.WebDriverWait(driver, 20) 
next_page_link2 = wait.until(
       EC.element_to_be_clickable((By.LINK_TEXT, 'Show more comments'))) 
next_page_link2.click() 

v = driver.find_elements_by_class_name('gig-comment-body') 
print(v) 
for h in v: 
    print(h.text) 

,但第二个按钮是无法点击,而给予例外:

selenium.common.exceptions.TimeoutException:

问题是什么?

+1

你能在这里这个链接分享HTML?或者你是否尝试过其他一些定位器?或者尝试使用'By.PARTIAL_LINK_TEXT' .. –

+0

@SaurabhGaur对于HTML,我给出了上面的链接,你可以去。我也尝试过其他选择器,但都是徒劳的。 – Ali

+0

这次我不能看到HTML,因为我不是PC,这就是为什么我要告诉你在这里分享链接HTML .. –

回答

0

我想你应该尝试使用execute_script()进行点击如下:

next_page_link2 = wait.until(
     EC.element_to_be_clickable((By.XPATH, '//div[contains(text(),"Show more comments")]'))) 

#now click this using execute_script 
driver.execute_script("arguments[0].click()", next_page_link2) 

希望它能帮助... :)

+0

@Saurah Gaur。 Noope。 – Ali

+0

@Ali是否有任何异常? –

+0

@Saurah Gaur。同样的例外:元素不可点击点(546.9833374023438,23.850006103515625)' – Ali

相关问题