【问题标题】:Element is not currently visible and so may not be interacted with[Selenium Webdriver+Python]元素当前不可见,因此可能无法与[Selenium Webdriver+Python] 交互
【发布时间】:2015-08-27 13:44:40
【问题描述】:

我正在实现一个使用 Selenium Webdriver 和 Python 来抓取一些网页的脚本,但我有一个特殊的问题。在某个时刻,我应该获取一个输入元素并发送一个密钥,并且在我的本地 PC 上脚本运行良好,而在我的服务器上,这个元素导致不可见。 页面的 HTML 部分是:

<div class="class1" style="width: 309px;">
   <div class="sPb-a">
      <div class="class2" style="display: none;" aria-hidden="true">
         www.lol.com
      </div>
   <input class="class3" type="text" dir="ltr" style="width: 301px;"></input>
   </div>
   <div></div>
</div>

在 Python 中,这是我的代码:

elem = browser.find_element_by_xpath("//input[@style='width: 301px;']")
elem.clear()
time.sleep(1)
elem.send_keys('lol')
time.sleep(1)

但是当我在元素上调用 clear 方法时,脚本会捕获以下错误:

Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpma637_/extensions/fxdriver@googlecode.com/components/command-processor.js:9982)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpma637_/extensions/fxdriver@googlecode.com/components/command-processor.js:12626)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpma637_/extensions/fxdriver@googlecode.com/components/command-processor.js:12643)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpma637_/extensions/fxdriver@googlecode.com/components/command-processor.js:12648)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpma637_/extensions/fxdriver@googlecode.com/components/command-processor.js:12590)

我还想多说两句:

  • 只有一个宽度为 301px 的元素,如果我尝试以其他方式捕获该元素,也会发生此错误,因此该元素是唯一的
  • 我的本地PC(脚本运行良好的地方)和我的服务器(有问题的地方)之间的配置是一样的(python版本、selenium版本、浏览器版本、操作系统版本)

您知道任何解决方案吗? 非常感谢

【问题讨论】:

  • 请添加更多信息。您使用的是什么浏览器和 webdriver 版本?您提供的代码之前的步骤是什么。这可以缩小问题的范围

标签: python selenium selenium-webdriver webdriver


【解决方案1】:

我能想到一些值得检查的事情。您可能需要稍等片刻才能加载页面。我在测试中遇到了一些服务器延迟问题,导致某些事情失败,因此值得检查。

人们倾向于遇到的另一件事是here。有时,当您的浏览器窗口中看不到某个元素时,您的 WebDriver 将无法找到它。这里有一些代码 sn-ps 用于滚动浏览器窗口(它们在链接中使用 Java,但切换到 Python 应该相当简单)。只需在使用 find_element_by_xpath() 之前删除该代码即可。祝你好运!

【讨论】:

  • 嗯不幸的是我已经验证了第一点插入一些等待时间但情况没有改变此外,从浏览器窗口中可以看到该元素.....我尝试了几个解决方案从其他答案,但他们不工作
  • 出于好奇,您使用 Xpath 而不是 find_element_by_class_name() 是否有特殊原因,您是否尝试过按类名使用此元素?
  • 就我而言,我可以通过直接使用find_element_by_class_name()selenium-python.readthedocs.io/locating-elements.html 中的其他人来解决一些问题,但总会有例外......
猜你喜欢
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
相关资源
最近更新 更多