Reputation: 9
I have this scenario:
I have multiple links on a single page , each link is inserted in a table. I extracted some
html code:
<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:
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
2) for each ID extracted I need to open the href link, if possible on a new page!
I'm trying all from yesterday , without any good result :(
Thank you all for each reply ;)
Upvotes: 0
Views: 538
Reputation: 46836
First you can get all of the links by creating a collection based on the static part of the 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.
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.
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
Upvotes: 4