Instead of building a collection of all links on the page, ie doing `browser.links`, you can use locators to build a collection of certain links. In this case, you would want to be filtering links based on the :href. For example, the collection would be `browser.links(href: /mysite\.com/)`.
Here is what the script might look like: require 'watir-webdriver' b = Watir::Browser.new :chrome # Go to the site b.goto 'mysite.com' # Find all of the links that include mysite.com in the href internal_links = b.links(href: /mysite\.com/) # Iterate through the links internal_links.each_with_index do |_, i| # Go back to the start page b.goto 'mysite.com' # Note that navigating to another page will invalidate our element references # As a result, we need to re-locate the link using the index b.link(href: /mysite\.com/, index: i).click # Perform actions on the page p b.url end - Justin Ko On Monday, June 29, 2015 at 1:40:30 PM UTC-4, Sergiu Cornea wrote: > > Good afternoon guys, > > I was wondering if someone could help me. > > Please correct me if I am wrong as I have just started learning Watir and > Ruby. > > Lets say I have 20 websites to be checked but I don't know the links so > what I am doing it is searching for them using the index value as follows: > > require 'watir-webdriver' > > b = Watir::Browser.new > b.goto "mysite.com" > > b.links.each_with_index do |_, i| > > b.link(index: i).click > end > > This piece of code will go and click on all the links that are available > on the Home page, however, I want it to just click on the internal links > only and not the external ones as well, for example "mysite.com/hello, > mysite.com/howareyou. > > I have found a work around which is that knowing what index the external > websites are you could skip them, however, I believe there must be a way of > skipping them by href or so, but because I am new I don't really know. > > Thank you in advance. > > Regards, > Cip > > This message and its attachments are private and confidential. If you have > received this message in error, please notify the sender and remove it and > its attachments from your system. > > The University of Westminster is a charity and a company > limited by guarantee. Registration number: 977818 England. > Registered Office: 309 Regent Street, London W1B 2UW. > -- -- Before posting, please read http://watir.com/support. In short: search before you ask, be nice. [email protected] http://groups.google.com/group/watir-general [email protected] --- You received this message because you are subscribed to the Google Groups "Watir General" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
