Java 在heroku上使用NoSuchElementException,但在本地主机上使用

Java 在heroku上使用NoSuchElementException,但在本地主机上使用,java,selenium,heroku,webdriver,webdriverwait,Java,Selenium,Heroku,Webdriver,Webdriverwait,我有一些奇怪的解析应用程序,可以在本地主机上完美运行,但当我将其上传到heroku时,它无法通过xpath找到元素。 它通过driver.get(“url”)进入页面(安装了chrome和chromedriver),我可以在日志currentUrl中看到,但它就是看不到元素 我尝试了线程。sleep(n)其中n甚至是120秒,我尝试过 WebDriverWait wait=newwebdriverwait(驱动程序,120)没有任何帮助 代码片段: options.setBinary("/app

我有一些奇怪的解析应用程序,可以在本地主机上完美运行,但当我将其上传到heroku时,它无法通过xpath找到元素。 它通过
driver.get(“url”)
进入页面(安装了chrome和chromedriver),我可以在日志currentUrl中看到,但它就是看不到元素

我尝试了
线程。sleep(n)
其中n甚至是120秒,我尝试过
WebDriverWait wait=newwebdriverwait(驱动程序,120)没有任何帮助

代码片段:

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();
当我在本地主机上使用它时,页面会打开,但是

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

在heroku的日志中,这里有几个可能解决此问题的步骤: 1.检查你是否在正确的页面上。 2.使用检查页面是否已完全加载

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);
}
private void waituntldocumentisready(TimeSpan超时)
{
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
var wait=new WebDriverWait(WebDriver,超时);
//检查文件是否准备好
Func readyCondition=webDriver=>js。
.ExecuteScript(“return(document.readyState='complete'&&jQuery.active==0)”;
等待。直到(就绪条件);
}
  • 尝试Javascript单击js.ExecuteScript(“参数[0]。单击();”,元素)
  • 根据最佳实践,当您的用例要在任何元素上调用
    click()
    时,一旦页面在
    driver.get()
    之后加载,而不是
    presenceOfElementLocated()
    ,您需要使用As
    元素tobelickable()
    如下所示:

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

    明白你的意思,用java编写了一个方法,但仍然是相同的错误(我只是不明白为什么它在localhost上工作,但在heroku上它得到了正确的页面url,但没有看到元素。这可能是一个解决问题,尝试最大化窗口