NoSuchElementException on heroku but works on localhost

Viewed 333

I have some oddsportal parsing app, that works perfectly on localhost, but when I upload it to heroku, it can't find an element by xpath. It goes to the page with driver.get("url")(chrome and chromedriver are installed), i can see in logs currentUrl, but then it just doesn't see elements

I've tried Thread.sleep(n) where n was even 120 seconds, have tried WebDriverWait wait = new WebDriverWait(driver, 120); Nothing helps

Snippet of code:

options.setBinary("/app/.apt/usr/bin/google-chrome"); 
System.setProperty("webdriver.chrome.driver","/app/.chromedriver/bin/chromedriver");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.oddsportal.com/results/#soccer");
WebDriverWait wait = new WebDriverWait(driver, 120);

element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@href='/soccer/austria/tipico-bundesliga/results/']")));
element.click();

Page opens when i use it on localhost, but

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:{"method":"xpath","selector":"//a[@href='/basketball/usa/nba/results/']"}

in logs on heroku

2 Answers

As per best practices, when your usecase is to invoke a click() on any element, once the page loads just after driver.get() instead of presenceOfElementLocated() you need to use the ExpectedConditions as elementToBeClickable() as follows:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/soccer/austria/tipico-bundesliga/results/']"))).click();

Here a few steps that might solve this issue: 1. Check you are on the right page. 2. Check the page is fully loaded by using

private void WaitUntilDocumentIsReady(TimeSpan timeout)
{
    JavascriptExecutor js = (JavascriptExecutor)driver;
    var wait = new WebDriverWait(WebDriver, timeout);            

    // Check if document is ready
    Func<IWebDriver, bool> readyCondition = webDriver => js.
        .ExecuteScript("return (document.readyState == 'complete' && jQuery.active == 0)");
    wait.Until(readyCondition);
}
  1. Try Javascript click js.ExecuteScript("arguments[0].click();", element)
Related