Ruby Watir:如何从单个页面打开多个链接

时间:2021-11-13 19:32:11

I have this scenario:

我有这种情况:

I have multiple links on a single page , each link is inserted in a table. I extracted some

我在一个页面上有多个链接,每个链接都插入一个表中。我提取了一些

html code:

HTML代码:

<td headers="record0_int1">
<a id="ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl00_PrimoLinkSSel"     title="Consulta la scheda dettagliata del bene con codice A241511" href="/secondasel.aspx?  id=815300&ida=241511&idp=473762&m=1&t=1">A241511</a>
</td>

<td headers="record1_int1">
<a id="ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl01_PrimoLinkSSel"     title="Consulta la scheda dettagliata del bene con codice A241512" href="/secondasel.aspx? id=815301&ida=241512&idp=473762&m=1&t=1">A241512</a>
</td>

What I'm looking for?

我在找什么?

I need to:

我需要:

1) get the id ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl00_PrimoLinkSSel , where for each link this ID changes only on a single number, example:

1)获取id ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl00_PrimoLinkSSel,其中对于每个链接,此ID仅在单个数字上更改,例如:

first id: ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl00_PrimoLinkSSel second id: ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl01_PrimoLinkSSel third id: ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl02_PrimoLinkSSel and so on

第一个id:ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl00_PrimoLinkSSel第二个id:ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl01_PrimoLinkSSel第三个id:ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl02_PrimoLinkSSel等等

2) for each ID extracted I need to open the href link, if possible on a new page!

2)对于提取的每个ID,我需要打开href链接,如果可能的话在新页面上!

I'm trying all from yesterday , without any good result :(

我从昨天开始尝试所有,没有任何好结果:(

Thank you all for each reply ;)

谢谢大家的回复;)

1 个解决方案

#1


4  

First you can get all of the links by creating a collection based on the static part of the id:

首先,您可以通过基于id的静态部分创建集合来获取所有链接:

links = browser.links(:id => /ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl\d+_PrimoLinkSSel/)

Then you can iterate through the links to click each one. You can do it like a user and hold down the control button to open a new page:

然后,您可以遍历链接以单击每个链接。您可以像用户一样执行此操作并按住控制按钮以打开新页面:

links.each { |link| link.click(:control) }

Alternatively, you can also force a new window by adding the target="_blank" attribute to each link before clicking it.

或者,您也可以通过在单击之前将target =“_ blank”属性添加到每个链接来强制新窗口。

links.each do |link|
  browser.execute_script('arguments[0].setAttribute("target", "_blank");', link)
  link.click
end  

For Firefox, the second approach seemed to be more stable since it does not rely on native events.

对于Firefox,第二种方法似乎更稳定,因为它不依赖于本机事件。

Once you have the pages open, you can get a specific window to work with. For example:

打开页面后,您可以获得一个特定的窗口。例如:

browser.window(:title => 'the window title').use do
  puts browser.text
end

You could also iterate through the windows, say to output each title:

您也可以遍历窗口,比如输出每个标题:

browser.windows.each do |window|
  window.use do
    puts window.title
  end
end

#1


4  

First you can get all of the links by creating a collection based on the static part of the id:

首先,您可以通过基于id的静态部分创建集合来获取所有链接:

links = browser.links(:id => /ctl00_ContentPlaceHolder1_Primasel2_1_dlstPrimasel_ctl\d+_PrimoLinkSSel/)

Then you can iterate through the links to click each one. You can do it like a user and hold down the control button to open a new page:

然后,您可以遍历链接以单击每个链接。您可以像用户一样执行此操作并按住控制按钮以打开新页面:

links.each { |link| link.click(:control) }

Alternatively, you can also force a new window by adding the target="_blank" attribute to each link before clicking it.

或者,您也可以通过在单击之前将target =“_ blank”属性添加到每个链接来强制新窗口。

links.each do |link|
  browser.execute_script('arguments[0].setAttribute("target", "_blank");', link)
  link.click
end  

For Firefox, the second approach seemed to be more stable since it does not rely on native events.

对于Firefox,第二种方法似乎更稳定,因为它不依赖于本机事件。

Once you have the pages open, you can get a specific window to work with. For example:

打开页面后,您可以获得一个特定的窗口。例如:

browser.window(:title => 'the window title').use do
  puts browser.text
end

You could also iterate through the windows, say to output each title:

您也可以遍历窗口,比如输出每个标题:

browser.windows.each do |window|
  window.use do
    puts window.title
  end
end