Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

text

click

watir

I'm not able to click on text link 'Add' using watir:

PAGE:

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

Watir CODE:

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

EXCEPTION:

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

Please help me how to handle this?

like image 635
chaitanya Avatar asked Apr 18 '13 11:04

chaitanya


People also ask

How can Watir help me find an element?

With Watir you can locate any element by its text, not just links, and Watir already supports partial matches for all of its locators with Regular Expressions. Most of the time Watir can translate the Selector Hash into a single XPath expression to quickly identify the element.

How can I find an element by its text?

These are :link_text and :partial_link_text . With Watir you can locate any element by its text, not just links, and Watir already supports partial matches for all of its locators with Regular Expressions. Most of the time Watir can translate the Selector Hash into a single XPath expression to quickly identify the element.

How do I find a link text in selenium?

Using Link Text In Selenium To Locate An Element In order to access link using link text in Selenium, the below-referenced code is used: driver.findElement (By.linkText ("this is a link text")); Note: In a scenario where, multiple links having similar text exists, it will automatically select the first one.

How does the Watir Location API work?

The Watir location API is designed to be easily read and understood by users (humans). Elements are located by creating a Selector Hash, which Watir translates into the potentially complicated information the driver needs to know to identify the element. The special cases will be highlighted below, but Watir Locators:


2 Answers

If the page has a lot of ajax and javascript going on, you may just have to wait a little bit for the client side code to finish rendering the page after it has been loaded from the browser.

Try this

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

If that does not work, then make sure the item is not in a frame or something..

btw, if there is more than one link on the page with the text 'Add' then you may have to specify a container outside the link that lets you identify which link you want. eg.

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

If

like image 61
Chuck van der Linden Avatar answered Sep 27 '22 22:09

Chuck van der Linden


This would be my way of doing it.

while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do
    browser.div(:class, 'containerDIV').a(:text, 'Add').click 
 end
like image 39
IT_puppet_master Avatar answered Sep 27 '22 21:09

IT_puppet_master