1

Hi Fairly new to watir and came across this problem. How can I select the button in the following snippet of code

<div id="side bar" class="sidebar">
<div class="inner active" data-reactid=".1">
<a class="back side bar" data-reactid=".1.0" href="#overview">
<h2 data-reactid=".1.1">
<div class="price clearfix" data-reactid=".1.2">
<div class="values type-current-value" data-reactid=".1.3">
<div class="values date-current-value" data-reactid=".1.4">
<div class="values duration-current-value" data-reactid=".1.5">
<div class="values passengers-current-value" data-reactid=".1.6">
<div class="values yacht-current-value" data-reactid=".1.7">
<div class="values flight-current-value" data-reactid=".1.8">
<div class="share-quote" data-reactid=".1.9">
<a class="share-quote cta-button cta-button-blue = share-quote-processed" data-reactid=".1.9.0" data-modal-url="/share-quote" href="#">Share this quote</a>
</div>

I am trying the following which produces a no method error

b.links(:xpath => '//div[@class="share-quote"]/a').to_a.click

rubytester
  • 37
  • 7

1 Answers1

2

The code is trying to click an array of links, rather than an individual link. That is why you get an undefined method error.

You need to click a specific link within the collection. For example:

# Click the first link
b.links(:xpath => '//div[@class="share-quote"]/a').first.click

# Click the last link
b.links(:xpath => '//div[@class="share-quote"]/a').first.click

# Click the nth link
b.links(:xpath => '//div[@class="share-quote"]/a')[n].click

Assuming there is only one of these links on the page, it would be more Watir-like to do:

b.div(class: 'share-quote').link.click
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Thanks for that. I found that it will only show that link up if the user moves down the page to the relevant section. So currently I have to manually move the vertical scroll otherwise the test will fail. – rubytester Apr 09 '16 at 13:03
  • 1
    There is the question [how to scroll a web page using watir](http://stackoverflow.com/q/13608890/1200545) that can help you with the scrolling question. – Justin Ko Apr 09 '16 at 16:30
  • Thanks again for your help – rubytester Apr 10 '16 at 14:24