RegEx a href link in Selenium Webdriver - ruby

2,245 views
Skip to first unread message

isonic1

unread,
Jan 4, 2012, 7:17:42 PM1/4/12
to Selenium Users
I have multiple links on the same page which have the same link text,
but different urls. Does anyone have an idea how I would go about
clicking a url that contains "site_12?", like below? I'd think to
regex the href link unless there is another option. Thanks in advance.

<span class="floatRight">
<a class="bookButton" title="Book Online" href="https://
www.somesite.com/branded/site_12?">
<img alt="Book Online" src="images/design/button-booking.jpg">


<span class="floatRight">
<a class="bookButton" title="Book Online" href="https://
www.somesite.com/branded/site_56?">
<img alt="Book Online" src="images/design/button-booking.jpg">

<span class="floatRight">
<a class="bookButton" title="Book Online" href="https://
www.somesite.com/branded/site_23?">
<img alt="Book Online" src="images/design/button-booking.jpg">

Daniel Gempesaw

unread,
Jan 4, 2012, 10:20:36 PM1/4/12
to seleniu...@googlegroups.com
Depending on what you're trying to do, you may want to look into using an HTML parser to help you get data out of HTML. I'm not really a Ruby guy so I'm sure someone else could provide more guidance, but from a quick google search there seems to be a nice Ruby HTML parser called Hpricot that could help you do this quite neatly. In general, using regex to parse HTML is A Bad Idea, but for simple things where you're sure you won't get caught by edge cases, using a regex may be faster.

All that being said, is there any reason why you can't use the following ?

element = driver.find_element(:css, 'a[href="https://www.somesite.com/branded/site_12?"]')
element.click

If your example is as simple as you described, something like /href="(.*)"/g should capture the url. 

Jayaraman - Inspired to become Software Architect

unread,
Jan 5, 2012, 1:19:51 AM1/5/12
to seleniu...@googlegroups.com
I would suggest to go with the xpath.Try to locate the first link using the below xpath
 
//a[contains(@href,'site_12)].  For Other links, replace site_56, site_23 in place of site_12 respectively.
 
In order to make a reusable one, locate the link with the above xpath but pass the number i.e 12 [For first link],56 [For second link],23 [For third link] as a parameter.
 
Ex:
//a[contains(@href,'site_${Parameter})].
 
Best Regards,
Jayaraman.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


bis

unread,
Jan 5, 2012, 1:42:40 AM1/5/12
to seleniu...@googlegroups.com
well if he is using site_## then wouldnt it make more sense to do this

click_link_with "site_12"
click_link_with "site_23"
click_link_with "site_56"


def click_link_with(link_number)
  all_links.each dop |my_link|
    if my_link.attribute('href').include? link_number
      my_link.click
    end
  end
end
 
def all_links
  @driver.find_element(:tag_name => 'a')
end


but to me it doesn't make sense to make things so difficult to test i would ask the dev's if it were possible to put an 'id' on the div higher up at least this is how i see the structrure i could be wrong

<div or ul tag>
    <div or li tag here>
       < div with the content that is around what we want> (If you add an id here that lets say contains the same site_12 because you have this info thats how they make the link below)

           <span class="floatRight">
               <a class="bookButton" title="Book Online" href="https://www.somesite.com/branded/site_12?">
                   <img alt="Book Online" src="images/design/button-booking.jpg">
               </a>
           </span>
       </div>
    </div>
</div>

then it would be a lot easier to interact with any of the elements in there. i may have gone a bit of course too many beers tonight and the ruby code is by ear. let me know if this is way off          

isonic1

unread,
Jan 5, 2012, 5:13:25 PM1/5/12
to Selenium Users
Thanks all for the sugestions. I'm definitely on the right track now
using xpath. I do want to make the links into a parameter as I can get
the values from the database. They originate as an array which I then
convert to a string. Perhaps my syntax is incorrect? Thanks again for
your help.

This works: @driver.find_element(:xpath, '//a[contains(@href,
"site_12")]').click

Does not work:
step 1. link = "site_12" (this is a string now)
step 2. @driver.find_element(:xpath, '//a[contains(@href,
link)]').click


On Jan 5, 1:19 am, Jayaraman - Inspired to become Software Architect
<electrical.i...@gmail.com> wrote:
> I would suggest to go with the xpath.Try to locate the first link using the
> below xpath
>
> //a[contains(@href,'site_12)].  For Other links, replace site_56, site_23
> in place of site_12 respectively.
>
> In order to make a reusable one, locate the link with the above xpath but
> pass the number i.e 12 [For first link],56 [For second link],23 [For third
> link] as a parameter.
>
> Ex:
> //a[contains(@href,'site_${Parameter})].
>
> Best Regards,
> Jayaraman.
>

isonic1

unread,
Jan 5, 2012, 5:25:44 PM1/5/12
to Selenium Users
Never mind. The solution was in front of my eyes all the time. Thanks
for the help.

@driver.find_element(:xpath, "//a[contains(@href, '#{link}')]").click
Reply all
Reply to author
Forward
0 new messages