最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】再次出现selenium.common.exceptions.ElementNotVisibleException Message element not visible

selenium crifan 7062浏览 0评论

【已解决】Selenium如何点击下拉框并选择某个值

之后,结果代码:

<code>checkoutElem = driver.find_element_by_xpath('//button[contains(@class, "cli-begin-checkout")]’)
checkoutElem.click()
</code>

又出现了一次

<code>    checkoutElem.click()
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/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.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.13.1 x86_64)
</code>

以为是需要等待,所以加了:

<code>time.sleep(5)
</code>

问题依旧。

但是这次很奇怪的是:

当前的checkout的button是能看得见的啊:

也没有visibility hidden之类的啊

不过更奇怪的是:

宽屏幕时只有一个checkout按钮

调试时,窄屏幕时,有两个:

selenium can see but ElementNotVisibleException Message  element not visible

python – Selenium webdriver – element can be found but is not visible? – Stack Overflow

说是该人用find_element_by_id其实是找到了多个元素

后续再去使用,则会发生:ElementNotVisibleException

那我这里也去确认一下是否有多个该条件找到的元素

但是此处通过log:

<code>checkoutElem=&lt;selenium.webdriver.remote.webelement.WebElement (session="a4ba1ab8024a42a01eb7051501b88f3c", element="0.23060781574162603-8")&gt;
</code>

是单个的WebElement,不是list啊

然后去Chrome调试看看,果然还真是:

第一个是看不见的

而第二个才是我们看到的checkout按钮

所以,此处要想办法去找到可见的那个

然后通过在某个父节点下再去找该子节点,就可以定位到我们需要的这个可见的元素了:

即:

可见的:

代码是:

<code>visibleCheckoutElem = WebDriverWait(driver, 10).until(
</code>

其中xpath的写法:

<code>//div[@class="order-summary"]//button[contains(@class, "cli-begin-checkout")
</code>

意思是:

先去找div中class=”order-summary”

然后再在其下找,//表示任意子节点中找,button,其中class包含了cli-begin-checkout的

就可以定位这个元素了。

而前面还有个不可见的按钮

也是之前写的条件:

<code>//button[contains(@class, "cli-begin-checkout")
</code>

的,其xpath,可以写成:

<code>//div[contains(@class, "checkout-mobile"]//button[contains(@class, "cli-begin-checkout")
</code>

【总结】

此处页面中:

肉眼看到的,其实就一个checkout的按钮,可以直接用:

<code>//button[contains(@class, "cli-begin-checkout")
</code>

的xpath去匹配到。

但是实际上页面的html中还存在另外一个不可见的button,也符合上面的条件的。

所以当时找到的,第一个符合上面条件的,是个不可见的button,所以调用click时报错:

ElementNotVisibleException: Message: element not visible

此处最后是用:

<code>//div[@class="order-summary"]//button[contains(@class, "cli-begin-checkout")
</code>

准确定位这个可见的checkout的button,然后再去click就不会报错了。

注:

对应着,之前那个不可见的button,其实可以用

<code>//div[contains(@class, "checkout-mobile"]//button[contains(@class, "cli-begin-checkout")
</code>

定位到的。

转载请注明:在路上 » 【已解决】再次出现selenium.common.exceptions.ElementNotVisibleException Message element not visible

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.175 seconds, using 22.10MB memory