4

On my web page, page loads but sub-tabs of page aren't clickable for 20 seconds (sometimes more than this). Page contents are -

<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've to click on sub-tab, hence I tried this in following way - Put sleep & then element.click But sleep is not ideal way to deal because sometimes it may happen that sub-tab element is clickable before or after the time given to sleep.

Using sleep, I did following -

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

If element is clickable after more than the sleep time & we click on element immediate after sleep time expires, (I mean (using above code) suppose element becomes clickable after 30 seconds but we click on element immediate after 20 seconds), actual click action doesn't happen & also click doesn't return any error.

Is there Ruby method to check whether element is clickable or not? So that we'll get to know when to click.

6
  • when you would try to click on an element, and if it is not available you should get an error. Did you get such? Commented Mar 26, 2013 at 17:05
  • No, it doesn't give an error. click gives an error only if element isn't present. Commented Mar 26, 2013 at 17:13
  • Reference - selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/… Commented Mar 26, 2013 at 17:14
  • Is there Ruby method to check whether element is clickable or not? - couldn't understand it. What do you mean by clickable? Commented Mar 26, 2013 at 17:18
  • We've method visible? to check whether element is visible. I'm looking for similar to check whether element is clickable. In my case, I'll wait till sub-tab element is clickable, once it's clickable, I'll click on it. Actually, I'm looking a way by which I can check element is clickable or not. It's not necessarily a method. Commented Mar 26, 2013 at 17:24

4 Answers 4

4

From 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 ordinarily you could 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 the click doesn't raise an error, it sounds like the click is in fact working, just your page isn't really ready to display that tab. I'm guessing there's some async javascript going on here.
So what you can try is inside the wait block, check that the click caused the desired change. I'm guessing, but you could 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 here is that #until will wait and repeat until the block gets a true result or the timeout is exceeded.

Sign up to request clarification or add additional context in comments.

1 Comment

Works brilliant
1

How about

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

1 Comment

could you refer to the documentation of it?
0

Hope it would help 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 could try this one:

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

6 Comments

When we say element.click & element isn't clickable (assuming element is present), it doesn't return StaleElementReferenceError (this is returned if the element no longer exists), it returns "". What I want is wait till element is clickable & then click on it.
No, this won't do I'm intending to do
@grm if the page load completed,then the element should present there. attempt to click on an element before page load causes something wrong.
click is same as clickAndWait for Ruby
I was looking for something like there is a way in Java. See comment on stackoverflow.com/questions/9878478/…
|
0

Here is what I use - to test if the link is clickable, else go to another URL:

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

1 Comment

Just realised this is question is for Ruby - answer is in C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.