元素当前不可见,因此可能无法与[Selenium Webdriver+Python]交互

2024-09-30 08:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在实现一个脚本,使用Selenium Webdriver和Python对一些网页进行爬网,但是我有一个特殊的问题。在某个时刻,我应该获取一个input元素并发送一个key,在我的本地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中,这是我的代码:

^{pr2}$

但当我对元素调用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版本、浏览器版本、操作系统版本)

你知道有什么解决办法吗? 非常感谢


Tags: div脚本com元素jscomponentsextensionsprocessor
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:14

我能想到一些值得检查的东西。您可能需要等待一段时间才能加载页面。我在测试中遇到了一些服务器延迟问题,导致一些东西失败,所以值得检查一下。在

人们倾向于遇到的另一件事是地址here。有时,当某个元素在浏览器窗口中不可见时,Web驱动程序将无法找到它。这里有一些用于滚动浏览器窗口的代码片段(它们是用Java编写的链接,但是转换到Python应该相当简单)。只需在使用find_element_by_xpath()之前删除该代码,这样就可以处理它了。祝你好运!在

相关问题 更多 >

    热门问题