【发布时间】: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