【问题标题】:How can I click second button on one webpage using selenium python3?如何使用 selenium python3 在一个网页上单击第二个按钮?
【发布时间】:2016-07-30 10:08:41
【问题描述】:

在 python、selenium、Win 7 中工作,我想点击位于 webPage 上的 sowcomment 按钮,该按钮由 wait 处理,然后我想点击 showmore Comments按钮以查看更多 cmets 以刮取更多 cmets。在第一个按钮之后,我可以提取 cmets。

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:

有什么问题?

【问题讨论】:

  • 你能在这里分享这个链接的 HTML 吗?还是您尝试过其他定位器?或者尝试使用By.PARTIAL_LINK_TEXT..
  • @SaurabhGaur 对于 HTML,我在上面提供了链接,您可以访问。我也尝试了其他选择器,但都是徒劳的。
  • 这次给定您的链接,我无法看到 HTML,因为我不在 PC 上,所以我告诉您在此处分享该链接 HTML..
  • @SaurabhGaur http://www.aljazeera.com/news/2016/07/generals-resign-turkey-military-council-160728055057094.html

标签: python-3.x selenium selenium-webdriver web-crawler


【解决方案1】:

我认为您应该尝试使用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)

希望对你有帮助...:)

【讨论】:

  • @Saurah Gaur 。没办法。
  • @Ali 有没有例外??
  • @Saurah Gaur 。同样的例外:Element is not clickable at point (546.9833374023438, 23.850006103515625)
  • @Ali 你正在使用.click() 发生这种情况,请尝试使用我在第二行提供的execute_script 点击...
  • 是的!我正在使用execute script
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2015-09-30
  • 2021-05-05
  • 2016-11-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多