How to click a link element with specific text using watir?

I canโ€™t click on the Add text link using watir :

PAGE:

<div id="divAdd" style="float: right"> <a onclick="SwitchView('2')" style="color: #1B56A7; cursor: pointer;">Add</a> </div> 

Cat.No .:

 browser.link(:text =>"Add").click 

AN EXCEPTION:

 Unable to locate element, using {:tag_name=>["a"], :text=>"Add"} 

Please help me how to handle this?

+4
source share
2 answers

If the page has a lot of ajax and javascript, you can just wait a bit for the client-side code to finish displaying the page after loading it from the browser.

try it

 browser.link(:text =>"Add").when_present.click 

If this does not work, make sure the element is not in the frame or something like that.

btw, if the page has more than one link with the text "Add", you may need to specify a container outside the link that allows you to determine which link you want. eg.

 browser.div(id: => "divAdd").link.when_present.click 

If

+12
source

That would be my way of doing this.

 while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do browser.div(:class, 'containerDIV').a(:text, 'Add').click end 
+1
source

All Articles