Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用watir webdriver访问循环中链接数组的href_Ruby_Selenium_Selenium Webdriver_Watir_Watir Webdriver - Fatal编程技术网

Ruby 如何使用watir webdriver访问循环中链接数组的href

Ruby 如何使用watir webdriver访问循环中链接数组的href,ruby,selenium,selenium-webdriver,watir,watir-webdriver,Ruby,Selenium,Selenium Webdriver,Watir,Watir Webdriver,我有一个特定的元素数组,如下所示: array = browser.as(:class => 'foo') $i = 0 while $i < num do browser.goto array[$i].href . . . $i += 1 end 我尝试访问以下链接: array = browser.as(:class => 'foo') $i = 0 while $i < num do browser.goto a

我有一个特定的
元素数组,如下所示:

array = browser.as(:class => 'foo')
$i = 0
while $i < num do
    browser.goto array[$i].href
    .
    .
    .
    $i += 1
end
我尝试访问以下链接:

array = browser.as(:class => 'foo')
$i = 0
while $i < num do
    browser.goto array[$i].href
    .
    .
    .
    $i += 1
end
以前

browser.goto array[$i].href

它显示终端窗口中第一个循环上的所有链接。

比我更了解这一点的人需要验证/澄清。我的理解是,在加载页面时,元素会被引用。加载新页面时,您正在构建所有新引用,即使每个页面上的链接看起来都一样

您正在存储对该元素的引用,然后要求它继续并从中提取href属性。它第一次工作,因为元素仍然存在。加载新页面后,它将不再存在。提取属性的请求失败

如果您只关心HREF,那么有一种更简短、更具Ruby风格的方法

array = []

browser.as(:class => 'foo').each { |link|
  array << link.href
}

array.each { |link| 
  browser.goto link
}
array=[]
as(:class=>'foo')。每个{|链接|

数组比我更了解这一点的人需要验证/澄清。我的理解是,在加载页面时,元素会被引用。在加载新页面时,您正在构建所有新引用,即使每个页面上的链接看起来都一样

您正在存储对该元素的引用,然后要求它继续并从中提取href属性。它第一次起作用,因为该元素仍然存在。加载新页面后,该元素不再存在。提取属性的请求失败

如果您只关心HREF,那么有一种更简短、更具Ruby风格的方法

array = []

browser.as(:class => 'foo').each { |link|
  array << link.href
}

array.each { |link| 
  browser.goto link
}
array=[]
as(:class=>'foo')。每个{|链接|

数组是,与存储定位器并在页面重新加载时重新查找它们的单个元素不同,元素集合只存储无法重新查找的Selenium元素

当您需要此模式且不仅仅是href值时,另一种解决方案是不创建集合,而是使用单个元素和索引:

num.times do |i|
  link = browser.link(class: 'foo', index: i)
  browser.goto link.href
end

是的,与存储定位器并在页面重新加载时重新查找它们的单个元素不同,元素集合只存储无法重新查找的Selenium元素

当您需要此模式且不仅仅是href值时,另一种解决方案是不创建集合,而是使用单个元素和索引:

num.times do |i|
  link = browser.link(class: 'foo', index: i)
  browser.goto link.href
end

第二次发生了什么?有错误吗?puts数组[1]的输出是什么。href?输出的形式为
http://www.foo.com
我收到的错误表明我无法使用
.href
运算符访问,但在循环中第一次运行良好。puts数组[0]、puts数组[0]的输出是什么。href和puts数组[1]?第二次发生了什么?有错误吗?puts数组[1]的输出是什么。href?输出的形式为
http://www.foo.com
我收到的错误表明我无法使用
.href
运算符访问,但在循环中第一次运行良好。puts数组[0]、puts数组[0]的输出是什么。href和puts数组[1]我明白了,这很聪明。我明白了,这很聪明。如何将
标记的复数化?
地址不起作用。如何将
标记的复数化?
地址不起作用。