有没有办法使用元素,虽然它不再在屏幕上?

时间:2013-04-21 11:32:19

标签: ruby watir watir-webdriver

找到元素后,我们说span。 有没有办法保存其属性或类似的东西,以便即使它不再存在于屏幕上,也可以“点击”span

我需要的是一种在执行某些操作后访问其指向的页面的方法,该操作会从屏幕中删除span

我知道watir-webdriver用于GUI测试,因此应该只允许人类可以执行的操作。但我希望还有办法。

2 个答案:

答案 0 :(得分:4)

我需要查看您网页的来源,但通常您可以这样做:

1.9.3p392 :001 > require "watir-webdriver"
 => true 
1.9.3p392 :002 > b = Watir::Browser.start "stackoverflow.com"
 => #<Watir::Browser:0x7f93652a09b4f1a2 url="http://stackoverflow.com/" title="Stack Overflow"> 

定义新变量

1.9.3p392 :004 > a = b.span(:id, "hlinks-custom").a(:text, "about").href
 => "http://stackoverflow.com/about" 

点击其他链接

   1.9.3p392 :005 > b.a(:text, 'faq').click
     => [] 
    1.9.3p392 :006 > b.title
     => "Frequently Asked Questions - Stack Overflow" 

最后重定向浏览器预设变量'a'

1.9.3p392 :007 > b.goto a
 => "http://stackoverflow.com/about" 

答案 1 :(得分:2)

是的,有一种方法,只需保存元素,并在以后使用它。以下是watir驱动google.com的示例,当您看到“mark ...”打印时,您可以关闭浏览器并看到变量“element”仍然保留其中的值......

  require 'watir';

  ie = Watir::Browser.new();
  ie.goto("google.com");
  puts("mark...");
  element = ie.span(:id, "gbqfsa").html();
  sleep(5);
  #close the browser while you sleep...
  puts(element);

它打印元素的html值,因此它仍然保持refence