ElementNotVisibleException:消息:元素不可见 - Python3 Selenium

ElementNotVisibleException: Message: element not visible - Python3 Selenium

我的任务是编写一个解析器来单击网站上看起来像按钮的 href link,但我遇到了一些问题。

这是 html:https://pastebin.com/HDKLXpdJ

来源html: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)

有关其工作原理的视频 link:

https://streamable.com/e1uvm

编辑:

问题已解决,查看@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中。另外,我在等待第一个元素,它恰好是不可见的。我需要找到第二个元素并等待它被点击。

您可以尝试点击

//span[@class='icn-phone icn-quaternary']

//div[@class='clearfix']

有几个问题。

  1. 您正在等待元素的出现。存在只是意味着该元素在 DOM 中,而不是它可见或可点击。如果您要等待并单击某个元素,请等待它可单击。如果您要等待 send_keys() 或从元素中获取文本,请等待它可见。这些是存在的一些用途,但我不经常使用它。话虽如此...

  2. 有两个元素与您的定位器匹配,id=reply-panel-reveal-btn。第一个匹配的恰好是不可见的。使用 XPath 我们可以创建一个定位器来找到第二个元素,等待它可点击,然后点击它。

    element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "(//a[@id='reply-panel-reveal-btn'])[2]")));
    element.click()