Cannot click html element with watir
Problem
From the comments we see that there are actually 51 td elements that have the text "Add Intrinsic Filter":
browser.tds(:text => 'Add Intrinsic Filter').length
#=> 51
We also see that some of these cells are visible and some are not - ie when calling .visible?
for each cell, some returned true while others returned false:
browser.tds(:text => 'Add Intrinsic Filter').map(&:visible?).uniq
#=> [false, true]
When locating a single element, Watir will pick the first element that matches. In this case, we can infer that the first cell with text "Add Intrinsic Filter" is not visible:
* The Selenium::WebDriver::Error::ElementNotVisibleError exception does it was not visible.
* Based on the ordering of the results of browser.tds(:text => 'Add Intrinsic Filter').map(&:visible?).uniq
, the first element was not visible.
Without seeing the page, we can only assume that the first matching cell is not the one you actually want to click.
Solution
You need to determine which of the 51 cells is actually the one you want to click and then use a more specific locator.
Based on the result from Selenium IDE, you could do:
browser.td(:xpath => '//tr[@id="filtersJob_intrinsic_container"]/td[2]/table[2]/tbody/tr/td[2]').click
Specifying the whole path to the element can be brittle to changes, so you might want to try something a little less specific. Perhaps try locating the row that has a specific id and then the cell with the specific text:
browser.tr(:id => 'filtersJob_intrinsic_container').td(:class => 'text', :text => 'Add Intrinsic Filter').click
Note that the :class
was added as a locator to try to get the inner td rather than the outer one.
Can Not Click The Same Element Class Using Watir
I guess all of these tabs are part of the same web page? I.e., all in the same HTML?
If that is the case, driver.button class: ['btn btn-primary']
is going to stop when it finds the first instance in the HTML, but that isn't the button you are looking for every time (it's the button in the first tab, where your script worked as you expected).
The best options in my mind are
- find a way to uniquely identify the button in each tab (for example, use
id
instead ofclass
if possible), or - pull all the buttons into a collection and click the button using its collection index after you figure out which index aligns with each tab. For example,
button_collection = browser.buttons(:class, ['btn', 'btn-primary'])
button_collection[2].click # Will click the 3rd button in the collection
Watir: my browser.button(:class='').click is not working
How many buttons with the same class are there in your page anyway?
browser.buttons(:class => "btn btn-add").count
usually when_present should take care of the timing issue, add another identifier to help locate the proper element
browser.button(:class => 'btn btn-add', :text => 'Add New User').when_present.click
Also try this (additional line of code, but could help with the timing issue):
Watir::Wait.until { browser.button(:class => 'btn btn-add').visible? }<br>
browser.button(:class => 'btn btn-add', :text => 'Add New User').click
You can also try regex to find your button
browser.button(:text => /Add New User/).when_present.click
Hope this helps
I can not locate proper element - click on link Ruby
There are two options. One is get your developers to add better IDs.
If that is not possible, try this:
how does ruby webdriver get element with hyphen in <name, value> pair
It worked for me in several similar situations.
I cannot select a link element using Watir
OK the mystery is solved. Iframes are a major gotcha when working with Watir. Elements that are part of an iframe are not visible unless you specifically select the iframe and then select the item in the iframe. So for example the code
browser.frame(:name, "nameOfFrame").link(:class, "Wizardbutton")
means give me the iframe with the name attribute "nameOfFrame" and then select the link with the class attribute of "Wizardbutton".
Watir-Webdriver, can click element in chrome but not in firefox
The different behaviour is due to how the FirefoxDriver implemented the clicking of an element compared to ChromeDriver. Based on my past observations:
- Chrome determines the centre of the element and then clicks the deepest element at those coordinates.
- Firefox determines the centre of the element and then clicks the centre of the element.
In other words, Firefox is clicking the article element. The click event only bubbles up, which means that the inner link never receives a click. In contrast, Chrome's algorithm will result in the link element being clicked first.
You can see the different behaviour in the following page. When elements that receive a click event will display an alert.
<html>
<head>
<script>
function highlight(elem) {
alert(elem.nodeName);
}
</script>
</head>
<body>
<div class="brands brands-search-region">
<section class="module" onclick="highlight(this);">
<div class="listing" onclick="highlight(this);">
<article onclick="highlight(this);" style="border:1px solid red; text-align:center;">
<a href="" style="border:1px solid green;" onclick="highlight(this);">asdf</a>
</article>
</div>
</section>
</div>
</body>
</html>
Run the following script using Chrome and Firefox:
browser = Watir::Browser.new :firefox # or :chrome
browser.goto 'path/to/file/test.htm'
browser.div(class: 'brands-search-region').article.click
bubbling = []
while browser.alert.exists?
bubbling << browser.alert.text
browser.alert.ok
sleep(1)
end
p bubbling
In Chrome, the bubbling
will be:
["A", "ARTICLE", "DIV", "SECTION"]
In Firefox, the bubbling
will be:
["ARTICLE", "DIV", "SECTION"]
Firefox has started the click event at the element you told it to click - ie the article element. In contrast, Chrome clicks like a user would - ie at the deepest element at a specific coordinate.
Related Topics
Unknown Ruby Interpreter Version (Do Not Know How to Handle): Ruby_Version
How to Get the Intersection, Union, and Subset of Arrays in Ruby
How to Get Activerecord Associations via Reflection
How to Get the Number of Days in a Given Month in Ruby, Accounting for Year
How to Drop to the Irb Prompt from a Running Script
Is There a Way in Ruby/Rails to Execute Code That Is in a String
What Does the "Yield" Keyword Do in Ruby
Error Installing Homebrew - Brew Command Not Found
How to Write Negative Loop in Ruby Like For(I=Index; I >= 0; I --)
How to Reference Global Variables and Class Variables
Mailer Unable to Access Reset_Token in User Model
Advantage of Tap Method in Ruby
Should I Define a Main Method in My Ruby Scripts
Finding Nil Has_One Associations in Where Query