'ChromeDriver Does not find the element on the page with selenium JAVA

I am trying to find an element on the page, I want to sorter the result list but my result show me that is not attached on the page:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=79.0.3945.130)
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'
System info: host: 'mac-etmc.local', ip: 'fe80:0:0:0:8d:ed57:9129:7f9c%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.130, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: /var/folders/n9/345_wf417mz...}, goog:chromeOptions: {debuggerAddress: localhost:62544}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 131de26bb0480d9c39dab037c54e0228
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    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.getAttribute(RemoteWebElement.java:134)
    at TestAmazon.main(TestAmazon.java:49)

That is the JAVA code to do that:

    //Order asc way 1
            WebElement listOrder = driver.findElement(By.id("a-autoid-0"));
            listOrder.click();
//Order asc way 2
WebElement listOrder = driver.findElement(By.xpath("//*[@id=\"a-autoid-0\"]"));
        listOrder.click();

//Order asc way 3
WebElement listOrder = driver.findElement(By.xpath("/html/body/div[1]/div[1]/span/h1/div/div[2]/div/div/span/form/span/span"));
        listOrder.click();

I noticed something strange, the SPAN in the first picture does not show on the page, only work like container, this is the problem? How I can to get this element with ChromeDriver?

enter image description here

enter image description here



Solution 1:[1]

Try these out:

  • CSS Selector: .a-button-text.a-declarative or #a-autoid-0-announce.
  • XPath: //*[@id="a-autoid-0-announce"].

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1