简体   繁体   中英

Java, Selenium, Chrome - Unable to click button, even though it seems to be able to find it just fine

I'm making a small bot for Cookie Clicker just to practice Selenium, and I have it working alright, the code is a mess, but that's not the problem, yet..
I have an issue accessing the options pane to load a savegame. My driver seems to be able to find the options button, but unable to click it.

private void loadGame() {
    System.out.println(driver.findElement(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")).getText());
    driver.findElement(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")).click();
}

It prints "Options", which is what leads me to believe it finds the element just fine, but throws an exception when I have it click in the next line

the print line is line 40, and the click line is line 41

Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) on port 36611
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
jun. 05, 2020 1:21:49 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Options
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=83.0.4103.61)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.61, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\Tobas\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:50074}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: bd980619f04c0e46fb6852e316304075
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at CookieClickerBot.loadGame(CookieClickerBot.java:41)
    at CookieClickerBot.start(CookieClickerBot.java:31)
    at Runner.main(Runner.java:9)

Process finished with exit code 1

Try using:

try{
 System.out.println(driver.findElement(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")).getText());
    driver.findElement(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")).click();
}catch(StaleElementReferenceException e){
 driver.findElement(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")).click();
}

It usually solves the issue, if it doesn't try the example given by: How to avoid "StaleElementReferenceException" in Selenium?

You can join waiting

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div[2]/div[18]/div[1]/div[1]")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM