【发布时间】:2018-03-25 14:06:31
【问题描述】:
我的任务是编写一个解析器来单击网站上的一个类似于按钮的 href 链接,但我遇到了一些问题。
这是 html:https://pastebin.com/HDKLXpdJ
这里是源代码:https://pastebin.com/PgT91kJs
Python 代码:
browser = webdriver.Chrome()
...
try:
element = WebDriverWait(browser, 20).until(
EC.presence_of_element_located((By.ID, "reply-panel-reveal-btn")))
finally:
elem = browser.find_element_by_xpath("//A[@id='reply-panel-reveal-btn']").click()
我收到此错误。
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
我尝试在 ChromeDriver 和 GeckoDriver(FF) 之间切换,但我一遍又一遍地遇到同样的错误。我什至尝试等待 10 秒加载,结果相同。
完整的错误文本:
File "C:/Users/DEM/PycharmProjects/Test/Scrape.py", line 46, in <module> elem = browser.find_element_by_xpath("//A[@id='reply-panel-reveal-btn']").click()
File "C:\Users\DEM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click self._execute(Command.CLICK_ELEMENT)
File "C:\Users\DEM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute return self._parent.execute(command, params)
File "C:\Users\DEM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute self.error_handler.check_response(response)
File "C:\Users\DEM\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64)
关于它应该如何工作的视频链接:
编辑:
问题已解决,请查看 @JeffC 答案。
正确的代码:
browser = webdriver.Chrome()
...
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "(//a[@id='reply-panel-reveal-btn'])[2]")));
element.click()
问题:
我在等待元素的出现。存在并不意味着元素可见或可点击,它只是意味着元素在 DOM 中。另外,我在等待第一个元素,它恰好是不可见的。我需要找到第二个元素并等待它被点击。
【问题讨论】:
-
是否需要向下滚动才能查看按钮?
-
有时,如果您不滚动,则不会生成动态内容。您可以在 JavaScript 中模拟这种行为
-
@Stack 你不需要向下滚动。
-
@rak007 你怎么能这样?
-
@rak007 谢谢你的好建议。这在某些情况下可能很有用,但在这种情况下,它不起作用。