1

I'm developing a program to automate some forms filling and I'm using Selenium. A problem that has been bugging me since the start if this work, is that an instruction that was supposed to click a button does not work and returns no error. Tis happens randomly, but it used to happen more frequently on the first time I run the program.

Basically the driver must enter a site, fill a popup window with the login and then when the first page loads, has to click a button, which will open a second page. What happens is that without opening the second page, the driver is already looking for an element that belongs to it.

At first I thought that the brower and the driver could be unsynchronized with the driver being faster than the browser and somehow messing up the whole process. I tried to correct this with a sleep of half a second. Also implemented an implicit waiting of 15 seconds.

Without any improvements, I decided to check if the first element is found. I've inserted a line to print the text on the button to the console and it is printed correctly. The next line would be to click that button, and although the driver retrieves no errors, on the browser, only the first page is loaded.

Last but not the least tried to find all elements with that xpath, but the list has only one element.

So, to sum up, the first element is found, the clicking instruction retrives no errors but still the button is not clicked. This happens randomly.

Here's the relevant part of the code I'm using:

tester.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    tester.get(website);
        Thread.sleep(1000);

        //Bypasses login pop-up window
        try
        {
            bypassLogin(username, password);
        }
        catch(Exception e)
        {
            handler(e.getMessage(), null);
            finisher(tester, br, br2, pathGecko, pathTemplate);
        }

        //Sleep in order to synchronize driver and browser
        Thread.sleep(1000);

        while((reader = br2.readLine()) != null)
        {
            tempOrig = new StringBuilder()
                            .append(tempOrig)
                            .append(reader)
                            .toString();
        }

        //Enters the body frame, with the elements to work with
        try
        {
            tester.switchTo().frame("portal_header");
            List <WebElement> LClick = tester.findElements(By.xpath(".//*[@id='2']/a"));
            System.out.println(LClick.size()); //Returns 1
            tester.findElement(By.xpath(".//*[@id='2']/a")).getText(); //Returns the correct value
            tester.findElement(By.xpath(".//*[@id='2']/a")).click();
            tester.switchTo().defaultContent();
            tester.switchTo().frame("portal_body");
        }
        catch(Exception e)
        {
            handler(e.getMessage(), null);
            finisher(tester, br, br2, pathGecko, pathTemplate);
        }
Hugo Torres
  • 308
  • 4
  • 13
  • Try using JavaScript click. `IWebElement element = tester.FindElement(By.xpath(".//*[@id='2']/a")); IJavaScriptExecutor js = (IJavaScriptExecutor)tester; js.ExecuteScript("arguments[0].click();", element);` With new version of Firefox and Gecko driver, I'm facing issues clicking on buttons. This solved my problem. Please give it a try. Code is in C#. – Sudeepthi Dec 16 '16 at 11:51
  • this code didn't work for me – Gülsen Keskin Dec 10 '21 at 08:39

2 Answers2

0

Try this and check

WebDriverWait wait = new WebDriverWait(tester, 15);
boolean bool = tester.findElement(By.xpath(".//*[@id='2']/a")).isDisplayed();
System.out.print(bool);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='2']/a")));
tester.findElement(By.xpath(".//*[@id='2']/a")).click();

also check browser version and selenium version

0
        IWebElement element = driver.FindElement(GridElementBy(gridId, rowIndex, colIndex));
        Scroll(element);
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript("arguments[0].click();", element);

Clicking using javascript doesn't work either

Gülsen Keskin
  • 657
  • 7
  • 23