使用Phantomjs Watir访问iFrame

时间:2014-09-02 21:43:00

标签: javascript ruby iframe phantomjs watir

我的脚本正在尝试访问此登录页面:

http://instagram.com/accounts/login

与PhantomJS,Watir。

登录页面的问题是登录文本字段位于iFrame中:

<iframe class="hiFrame" data-reactid=".0.0.0.1.0.1.0.0.$frame" src="https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com" scrolling="no" seamless="">
  #document
  <!DOCTYPE html>
  <html class="hl-en not-logged-in " lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body class="LoginFormChrome ">
      <div class="LoginFormPage" data-reactid=".0">
        <form data-reactid=".0.0">
          <p class="lfField" data-reactid=".0.0.0">
            <label class="lfFieldLabel" data-reactid=".0.0.0.0"></label>
            <input class="lfFieldInput" type="text" data-reactid=".0.0.0.1" value="" autocorrect="false" autocapitalize="false" maxlength="30" name="username"></input>
          </p>
          <p class="lfField" data-reactid=".0.0.1"></p>
          <p class="lfActions" data-reactid=".0.0.2"></p>
        </form>
      </div>
      <div id="fb-root" class=" fb_reset"></div>
    </body>
  </html>
</iframe>

这是我的代码(Ruby):

B.goto HOME_URL + LOGIN
sleep(5)            
puts B.iframe(:class => "hiFrame").text_field(:name => "username").exists?
B.iframe(:class => "hiFrame").text_field(:name => "username").set USERNAME
B.iframe(:class => "hiFrame").text_field(:name => "password").set PW
#puts B.iframe(:class => "hiFrame").text_field(:name => "username").exists?
B.iframe(:class => "hiFrame").button.click

使用Firefox浏览器完美运行,但当我切换到Phantomjs时,它无法识别

B.iframe(:class => "hiFrame").text_field(:name => "username").exists?

并停止脚本。返回&#34; true&#34;为:

B.iframe(:class => "hiFrame").exists?

但那是它的程度。如何使用phantomjs和watir登录Instagram?

感谢。

1 个答案:

答案 0 :(得分:0)

尝试在进入页面之前在代码中添加此内容:

B = Watir::Browser.new :phantomjs
B.window.resize_to(800, 600)

如果设置窗口大小,它应该可以工作。 Phantomjs通常需要指定浏览器大小。

B.window.resize_to(800, 600)
B.goto HOME_URL + LOGIN
sleep(5)            
puts B.iframe(:class => "hiFrame").text_field(:name => "username").exists?
B.iframe(:class => "hiFrame").text_field(:name => "username").set USERNAME
B.iframe(:class => "hiFrame").text_field(:name => "password").set PW
#puts B.iframe(:class => "hiFrame").text_field(:name => "username").exists?
B.iframe(:class => "hiFrame").button.click