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

【已解决】selenium.common.exceptions.ElementNotVisibleException Message element not visible

selenium crifan 3353浏览 0评论

折腾:

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

期间,代码:

<code>cartNumOptionElemList = driver.find_elements_by_xpath('//ul[@class="dropdown-menu"]/li[@role="option"]')
</code>

想要点击

<code>&lt;li role="option"&gt; 
&lt;a data-m="{&amp;quot;aN&amp;quot;:&amp;quot;shoppingCart&amp;quot;,&amp;quot;cN&amp;quot;:&amp;quot;UpdateQuantity&amp;quot;,&amp;quot;bhvr&amp;quot;:91,&amp;quot;pid&amp;quot;:&amp;quot;8X58XHDX57SX&amp;quot;,&amp;quot;sku&amp;quot;:&amp;quot;F96R&amp;quot;,&amp;quot;itemCount&amp;quot;:5}" id="59ad574f-26c2-466e-9794-28553e40fc2f_menuItem_5" class="ember-view x-hidden-focus"&gt;                          5
                      
&lt;/a&gt;            &lt;/li&gt;
</code>

结果报错:

<code>selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
</code>

然后以为是此处的

<li>不能点击呢。

结果去找到其下的<a>:

结果错误依旧。

selenium ElementNotVisibleException: Message: element not visible

好像是需要鼠标移动上去?才能点击?

python – Selenium Element not visible exception – Stack Overflow

python – selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with – Stack Overflow

selenium遇到异常:Element is not currently visible and so may_萩茝_新浪博客

(1)python – Selenium无法定位到指定元素:Element is not currently visible ? – SegmentFault

How to Solve ElementNotVisibleException in Selenium Webdriver

此处觉得奇怪的是:

此时对应的可点击的列表是显示出来的:

-》

不过调试期间注意到:

如果在Chrome窗口和PyCharm中切换,则会导致弹出的列表消失缩回去了:

-》或许是调试期间导致的?

那么去掉断点,直接运行看看效果

果然是:

去掉断点:

直接运行,然后就可以看到 下拉菜单被点击出来后,然后选择了希望选择的5:

【总结】

此处想要点击clik某个列表选项时报错:

<code>selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
</code>

的原因是:

在给PyCharm加了断点调试,使得到了断点,屏幕焦点从Chrome浏览器切换到PyCharm中

导致之前Chrome中获得了焦点的,已经被点击而展开了的选项列表,缩回去了,从而变得不可见

导致后续代码,虽然拿到了element元素,但是却click时报不可见的错误。

解决办法:

去掉PyCharm的断点,确保Chrome浏览器正常显示和运行,不要被打扰,则代码就可以点击,可见的,已经展开的选项列表了。

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

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.160 seconds, using 22.08MB memory