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?
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.
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.
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.
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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With