Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I tried to find a solution to this thing and I spent a lot of time, but it is almost impossible for 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 CSS selector, 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 thought 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 images of the element:

Element 1 Element 2

Does anybody know how to do that?

Please log in or register to answer this question.

Browse Categories

...