How to determine that an element can be clicked using Selenium WebDriver with Ruby?

Pages are loading on my webpage, but the sub-tabs of the page are not available for 20 seconds (sometimes more). Page Content -

<nav id="subTabHeaders"> <div class="selected" data-name="ab">AB</div> <div class="" data-name="cd">CD</div> <div class="" data-name="ef">EF</div> <div class="" data-name="gh">GH</div> </nav> 

I have to click on the sub-tab, so I tried it this way: Put a dream and then element.click But a dream is not an ideal way to handle it, because sometimes it can happen that a sub-tab element can be clicked before or after the time that assigned for sleep.

Using a dream, I did the following -

 element = WAIT.until { driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]")} sleep 20 element.click 

If an element is clickable after a longer sleep time, and we click on the element immediately after the timeout (I mean (using the code above), suppose the element becomes clickable after 30 seconds, but we click on the element immediately after 20 seconds) , the actual action of the click does not occur, and also the click does not return any errors.

Is there a Ruby method for checking if an element is clickable or not? So we know when to click.

+4
source share
4 answers

On the ruby bindings page: (see driver examples)

 # wait for a specific element to show up wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds wait.until { driver.find_element(:id => "foo") } 

So you can do something like:

 wait = Selenium::WebDriver::Wait.new(:timeout => 40) wait.until do element = driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]") element.click end 

Or more succinctly

 wait = Selenium::WebDriver::Wait.new(:timeout => 40) wait.until { driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]").click } 

However, since you say that clicking does not cause an error, it looks like clicking actually works, it’s just that your page is not really ready to display this tab. I assume there is asynchronous javascript here.
So, what you can try is inside the wait block, make sure that the click causes the desired change. I suppose, but you can try something like:

 wait = Selenium::WebDriver::Wait.new(:timeout => 40) wait.until do driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]").click driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3][@class='selected']") end 

The important thing is that #until will wait and repeat until the block gets the true result or the timeout is exceeded.

+2
source

What about

 WebDriverWait wait = new WebDriverWait(driver,30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(subTabHeaders))); 
+1
source

Hope this helps you:

 begin element = WAIT.until { driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]")} sleep 20 element.click rescue Selenium::WebDriver::Error::StaleElementReferenceError p "Indicates that a reference to an element is now "stale" - the element no longer appears in the DOM of the page." end 

OR

you can try the following:

 begin wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds wait.until { driver.title.include? "page title" } driver.find_element(:xpath, ".//*[@id='subTabHeaders']/div[3]")}.click rescue Selenium::WebDriver::Error::StaleElementReferenceError p "element is not present" end 
0
source

Here is what I use - to check if the link is available, otherwise go to another URL:

  if (logOutLink.Exists() && ExpectedConditions.ElementToBeClickable(logOutLink).Equals(true)) { logOutLink.Click(); } else { Browser.Goto("/"); } 
0
source

All Articles