3

I have written a java program that utilizes Selenium to control Google Chrome. If I run the program on my machine, the program runs properly and at a reasonable speed. However, when I export this file to another machine (as a .jar or .exe), it still runs properly but is extremely slow. Specifically, it seems to take much longer to write in text boxes and click on buttons.

Throughout the program, I call this method to ensure that the text box/label will exist before an action occurs:

/**
 * This function allows the driver to wait until the element is visible before interacting with it 
 * @param driver - The driver that is being used
 * @param webElement - A string describing the xpath of the element to wait for 
 * @param seconds - The maximum number of seconds that the driver is willing to wait
 * @return - The WebElement after it has appeared on the page 
 */
public static WebElement waitForElementToBeVisible(WebDriver driver, String webElement, int seconds) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, seconds);

        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(webElement))); 

        return element;
    } 
    catch (TimeoutException e){
        return null; 
    }

}

And, I call this method to ensure that the button is clickable before calling the action:

/**
 * This function allows the driver to wait until the element is clickable before interacting with it 
 * @param driver - The driver that is being used 
 * @param webElement - A string describing the xpath of the element to wait for 
 * @param seconds - The maximum number of seconds that the driver is willing to wait
 * @return - The WebElement after it has appeared on the page 
 */
public static WebElement waitForElementToBeClickable(WebDriver driver, String webElement, int seconds) {
    WebDriverWait wait = new WebDriverWait(driver, seconds); 

    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(webElement)));

    return element; 
}

From my research, I attempted to fix the problem in a variety of ways. First, I attempted to adjust the Proxy settings through Google Chrome, but the settings were the same on both machines. Second, I tried to set the proxy settings programmatically but that also did not speed up the program. Finally, I tried to reduce the time that is passed in to the methods above. Unfortunately, that did not speed up the program either. Does anyone know how to have similar performance of Java Selenium programs on every machine?

Thank you in advance for any help that you are able to provide.

Mitch Kelly
  • 481
  • 3
  • 10
  • You say you want to check if an element "exists" but you're using a wait until visible, which isn't quite the same thing. You can use `.findElements(` to check if an element exists or not, and rather quickly, as it does not use the implicit wait defined for timeout, it will return zero immediately if the element does not exist. Not saying you should necessarily change to that, but that is one way to speed it up (a little). You might be misled on your perceived speed issues. I run my scripts and get different speeds almost every time, including ones that "crawl" when sending text. – Bill Hileman Jul 09 '19 at 19:54

0 Answers0