Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium / Firefox: Command ".click()" doesn't work with a found element

Tags:

I tried to find a solution to this thing and I spent a lot of time, but it is almost imposible to me to do that.

The matter: I am using Selenium with Java in Firefox. I need to find an element (a listbox) and click on it. So, the code finds the element, but click action does not work. It works fine in Google Chrome every time, and just sometimes in Firefox (with the same Java code sometimes works, and sometimes does not).

There is the part of code with the element when the program enters on the page:

    <div id="size-btn" class="size-btn">
      <span class="selected-size">SELECCIONA TALLA </span>
      <div class="size-select" style="display: none;">
        <table>
          <tbody>
            <tr id="selecsize_2" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051607">
            <tr id="selecsize_3" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051608">
            <tr id="selecsize_4" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051609">
            <tr id="selecsize_5" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051610">
          </tbody>
        </table>
      <button class="size-guide gaViewEvent gaTrack" data-ga-props="{action:'Seleccionar_Talla', opt_label:'Guia_de_tallas'}" data-href="http://www.anyweb.com/webapp/wcs/stores/servlet/ProductGuideSizeAjaxView?catalogId=24052&categoryId=358056&langId=-5&productId=1047599&storeId=10701">Guía de tallas</button>
      </div>
    </div>

And there is the part of code that changes when the element is clicked:

    <div id="size-btn" class="size-btn opened">

I tried many solutions and sometimes it works, but the next time I run the program, it does not work again.

Some solutions:

  1. It finds the element, but does not run click action. I checked with xpath and cssSelector, and there are unique elements found with those expressions.

    driver.findElement(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span")).click(); // Also checked with By.cssSelector("span.selected-size")
    
  2. I though it was because of the time, so I tried to solve it that way.

    WebElement we = driver.findElement(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span")); // By.cssSelector("span.selected-size")
    Thread.sleep(3000);
    we.click();
    
  3. Finally, I was a little bit desperate, and I created a new function to try to do this almost 60 times, looking for the change on the element code and if there was any change, just tried to do click action again.

    clickAndWaitWhileElementIsNotPresent(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span"),By.xpath("//div[@class='size-btn opened']/span")); // By.cssSelector("span.selected-size")
    
    private void clickAndWaitWhileElementIsNotPresent(By by1, By by2) throws Exception {
        for (int second = 0;; second++) {
            if (second >= 60)
                fail("timeout");
            try {
                if (isElementPresent(by2))
                {
                    break;
                }
                else
                {
                    driver.findElement(by1).click();
                }
            } catch (Exception e) {
            }
            Thread.sleep(1000);
        }
    }
    

There are the images of the element:

Element 1Element 2

Does anybody know how to do that?

like image 281
ovejaexiste Avatar asked Mar 08 '13 13:03

ovejaexiste


Video Answer


2 Answers

Finally I found an answer that works with Firefox as well as Google Chrome.

WebElement we = this.driver.findElement(By.id("size-btn"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", we);

waitForElementPresent(By.xpath("//div[@id='size-btn' and contains(@class,'opened')]/span"));
like image 135
ovejaexiste Avatar answered Sep 27 '22 21:09

ovejaexiste


I am not sure why are you using this Xpath, if you have freedom to change Xpath then record the element using selenium IDE and use Xpath::position from drop down list of target(it picks unique path relative to html header), it will solve problem of dynamic locator. And try below mentioned events.

1- Use clickAt.

2- Use fireevent(focus) and then click. Sometime it happens some element in back ground is getting loaded, when it gets loaded, focus move there hence elementNotVisible error.

3- Use mouseDownRight.

like image 36
Amit Shakya Avatar answered Sep 27 '22 20:09

Amit Shakya