运行代码时,陈旧元素引用错误

时间:2019-01-16 21:39:18

标签: staleelementreferenceexception

我是Java和Selenium的新手。可以做一些实践,但是当我运行代码时,我得到“陈旧元素引用:元素未附加到页面文档中”

我一直在寻找解决方案,但到目前为止没有任何效果。我使用的页面是 https://www.cricbuzz.com/live-cricket-scorecard/20303/aus-vs-ind-3rd-test-india-tour-of-australia-2018-19

页面会刷新时间,我相信这就是为什么我得到此错误。

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class HTML5_Tables {

public static void main(String[] args) {
    // TODO Auto-generated method stub
int sum=0;  //used for sum up
    System.setProperty("webdriver.chrome.driver", "C:\\Download\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.get("https://www.cricbuzz.com/live-cricket-scorecard/20303/aus-vs-ind-3rd-test-india-tour-of-australia-2018-19");
    //Thread.sleep(5000);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


    WebElement table=driver.findElement(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']"));
    int rowcount=table.findElements(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).size();
    int count=table.findElements(By.cssSelector("div[class='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)")).size(); //traverse from parent to child and child index


    // how to iterate through all the elements with the common css
    for (int i=0; i < count-2; i++) {
        String value=table.findElements(By.cssSelector("div[class='cb-col cb-col-100 cb-scrd-itms'] div:nth-child(3)")).get(i).getText();
    int valueinteger = Integer.parseInt(value); //converting string to integer
            sum=sum+valueinteger; //sum up logic
    }
    String Extras=driver.findElement(By.xpath("//div[text()='Extras']/following-sibling::div")).getText();
    int extraValue=Integer.parseInt(Extras);
    int TotalSumvalue=sum+extraValue; //sum up logic
    System.out.println(TotalSumvalue);

    String ActualTotal=driver.findElement(By.xpath("//div[text()='Total']/following-sibling::div")).getText(); //traverse from child to child using xpath and getting 'text' value using xpath. 
    int ActualTotalValue=Integer.parseInt(ActualTotal);
    if (ActualTotalValue==TotalSumvalue) {
        System.out.println("Count matches");
    }
    else{
        System.out.println("Count does not match");
    }
}

我正在尝试汇总该表中“ R”列中的所有值,但出现此错误:

Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 33117
Only local connections are allowed.
Jan 16, 2019 1:26:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
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:53'
System info: host: 'TRTQAWS14-SDLA', ip: '172.17.25.98', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.45.615291 (ec3682e3c9061c..., userDataDir: C:\Users\smolyan\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:49800}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 71.0.3578.98, webStorageEnabled: true}
Session ID: a078d04cffdda529d2bcf178dec0407c
*** Element info: {Using=css selector, value=div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    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.findElements(RemoteWebElement.java:204)
    at org.openqa.selenium.remote.RemoteWebElement.findElementsByCssSelector(RemoteWebElement.java:257)
    at org.openqa.selenium.By$ByCssSelector.findElements(By.java:441)
    at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:177)
    at HTML5_Tables.main(HTML5_Tables.java:22) 

第22行是:

int rowcount=table.findElements(By.cssSelector("div[class='cb-col cb-col-100 cb-ltst-wgt-hdr']")).size();

0 个答案:

没有答案