Home > Back-end >  Selenium Actions.click() with no wait Java Chromedriver
Selenium Actions.click() with no wait Java Chromedriver

Time:10-12

I'm writing a selenium script that interacts with one of our systems. In our system, there is a button that, when pressed, only loads the page correctly about half the time (It just hangs on loading for an infinite amount of time on every other occasion) and therefore I cannot rely on the default selenium wait for the page to load. For this reason, I would like to press the button, and close the web browser immediately before opening up a new window to continue on with the script. Unfortunately I have no ability to fix the issue with the button itself.

My current code:

Actions action = new Actions(driver);
        
WebElement webElement = driver.findElement(By.id("garbage_button"));
        
action.moveToElement(webElement).click();
        
action.perform();

System.out.println("It's worked");

Currently, the System.out will unlikely be reached because once the action.perform() is run the page just infinitely hangs and my script eventually times out.

Other things I have tried:

  • WebElement.click() instead

  • Reducing the wait time and attempting to catch the time out exception (Chromedriver seems to handle these exceptions itself)

Does anyone have any ideas on how I could achieve a button press without any wait?

CodePudding user response:

action.perform(); should be action.build().perform(); in Java.

Once you do that why not to trigger driver.quit() to close the windows ?

or you can possibly have an assert when the click in triggered, you could assert something from next page and if assertion pass then test case should pass as well.

But since you've mentioned many times new page won't load, in those situation your assertion will fail, and that should be okay cause actual page did not load at all.

CodePudding user response:

In our system, there is a button that, when pressed, only loads the page correctly about half the time (It just hangs on loading for an infinite amount of time on every other occasion) and therefore I cannot rely on the default selenium wait for the page to load.

You can load the page faster by doing this. This will make Selenium WebDriver to wait until the initial HTML document has been completely loaded and parsed, and discards loading of stylesheets, images and subframes.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER);

You can kill driver session by using below command.

Runtime.getRuntime().exec("taskkill /F /IM ChromeDriver.exe");
  • Related