Question

I am trying to run automated test scripts (Selenium Webdriver2 + ruby) but facing a weird problem off late. The scripts that worked perfectly fine till yesterday is throwing 'No such element exception' now. However, the path certainly exists when checked in firebug and theres no change in the application what so ever. The script fails in detecting iframe2 in below code: -

browser.manage.timeouts.implicit_wait = 20 # seconds

                                                        ############ GO TO OVERVIEW TAB ################

#Adding wait until quote is created and page is ready for content tab click.
wait = Selenium::WebDriver::Wait.new(:timeout => 5)
wait.until { browser.find_element(:id => "j_id0:tabDetailedContent_lbl") }

browser.find_element(:id => "j_id0:tabDetailedContent_lbl").click



iframe = browser.find_element(:id =>'CPQFrame')
browser.manage.timeouts.implicit_wait = 10
browser.switch_to.frame(iframe) 

browser.find_element(:css,".processBarElement.noSelected").click


#frame.browser.find_element(:css,".processBarElement.noSelected").click

#browser.manage.timeouts.implicit_wait = 30 # seconds

iframe2 = browser.find_element(:xpath,'html/body/div[3]/div[2]/div[2]/div[3]/iframe')

#browser.manage.timeouts.implicit_wait = 10
browser.switch_to.frame(iframe2)

I googled for such inconsistent behavior but could not find any reasonable solution. One post was dated back to 2009 which blames unstable Selenium Webdriver.

Has anybody else experienced this too? Any workaround / solutions?

Help out guys!

Thanks.

Abhishek

Was it helpful?

Solution 2

This is less likely to be an issue with Webdriver being unstable but with the execution timing behaving differently than before. I would suggest taking a look at using explicit waits for some of the elements that you are having a hard time finding. You can read about them here in the selenium documentation.

This is the seleniumhq example:

require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get "http://somedomain/url_that_delays_loading"

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
  element = wait.until { driver.find_element(:id => "some-dynamic-element") }
ensure
  driver.quit
end

OTHER TIPS

I have faced the same problem. The DOM looses the reference to the element in question. It can either be StaleStateReferenceException or NoSuchElementException. There are two ways to deal with the situation. (Though my solution is in Java. The underlying concept is the same. )

By using the the following method, you can try clicking an element. If exception is thrown then catch the exception and try to click again until the element is present:

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            Actions action = new Actions(driver);
            WebElement userClick = wait.until(ExpectedConditions.presenceOfElementLocated(by));
            action.moveToElement(userClick).click().build().perform();
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementReferenceException e) {
            System.out.println("StaleElementReferenceException");
        }
        catch(NoSuchElementException e) {
            System.out.println("No Such Element Found");
        }
        attempts++;
    }

    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top