【问题标题】:Unable to perform any action using phantomjs watirwebdriver无法使用 phantomjs watirwebdriver 执行任何操作
【发布时间】:2015-05-19 00:07:42
【问题描述】:

我无法使用 phantomjs webdriver 无头执行在网页上执行任何操作,例如填充文本框、单击按钮等。

require 'watir-webdriver'
b=Watir::Browser.new :phantomjs
b.goto "google.com"
b.title # Google
b.text_field(:id => "lst-ib").set "Avinash"

所以在填写文本框时,我面临以下问题。

Watir::Exception::UnknownObjectException: unable to locate element,
using {:id=>"lst-ib", :tag_name=>"input or textarea", :type=>"(any
text type)"}    from
/Library/Ruby/Gems/2.0.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/elements/element.rb:508:in
`assert_exists'     from
/Library/Ruby/Gems/2.0.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/user_editable.rb:11:in
`set'   from (irb):16   from /usr/bin/irb:12:in `<main>'

在没有无头执行的情况下使用 firefox 时,相同的脚本可以正常工作。如果有任何建议,请告诉我。

以下是我的 gem 版本:
1. watir-webdriver (0.6.10)
2. selenium-webdriver (2.45.0, 2.43.0)
3.phantomjs 2.0.0

【问题讨论】:

  • 没有成功。即使在最大化浏览器之后也是如此。
  • 我不使用 phantomjs,并且在您给出的错误中没有太多信息可以解决。我建议更新到最新的 watir-webdriver 版本 (0.7.0),但这不太可能是您的实际问题。
  • 尝试改用:name =&gt; 'q'。结果是什么?此外,您已验证 b.title"Google"

标签: ruby selenium watir watir-webdriver


【解决方案1】:

主要问题似乎是 PhantomJS 看到的 HTML 与 Firefox 不同。

在 Firefox 中,文本字段的 HTML 是:

<input spellcheck="false" dir="ltr" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" aria-autocomplete="both" role="combobox" aria-haspopup="false" class="gsfi lst-d-f" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" value="" aria-label="Search" type="text">

对比 PhantomJS 显示:

<input style="color: rgb(0, 0, 0); margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 8px; padding-bottom: 0px; padding-left: 6px; vertical-align: top; outline-style: none; outline-width: initial; outline-color: initial; " autocomplete="off" class="lst" value="" title="Google Search" maxlength="2048" name="q" size="57" dir="ltr" spellcheck="false">

注意 PhantomJS 没有 id 属性。因此,通过 ID 定位字段是有道理的。

您将需要使用不同的定位器。例如:

b.text_field(:name => "q").set("Avinash")

b.text_field(:title => "Google Search").set("Avinash")

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 2011-02-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多