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

时间:2017-10-13 12:23:45

标签: python selenium scrape

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

这是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)

视频链接,了解它应如何运作:

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 中。此外,我正在等待第一个元素,它恰好是不可见的。我需要找到第二个元素,然后等待它被点击。

2 个答案:

答案 0 :(得分:1)

有几个问题。

  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()
    

答案 1 :(得分:0)

你可以尝试点击

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

//div[@class='clearfix']
相关问题