从<li>中选择下拉值

我有一个包含许多选项的下拉列表,在这些下拉列表中,我必须选择一个选项并单击它们,为此,我正在使用以下代码,但这给了我错误。任何人都可以帮忙。

“线程“主”中的异常org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档”

下面是我编写的代码。

driver.findElement(By.cssSelector("#select2-religion-container")).click(); //religion dd click

        List <WebElement> listReligion = driver.findElements(By.cssSelector("#select2-religion-results>li"));

        System.out.println(listReligion.size());

        String MyReligion = "Christian";
        driver.findElement(By.cssSelector(".select2-search__field")).sendKeys(MyReligion);
        for(int i=0 ; i< listReligion.size() ; i++) {
            if(list.get(i).getText().equals(MyReligion)) {
                list.get(i).click();
            }
        }

下面是我得到的错误。

Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 27881
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1589711914.821][WARNING]: This version of ChromeDriver has not been tested with Chrome version 81.
[1589711916.986][WARNING]: Timed out connecting to Chrome, retrying...
May 17, 2020 4:08:39 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=81.0.4044.138)
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:17:03'
System info: host: 'DESKTOP-1QASA0A', ip: '192.168.1.129', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '14'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 81.0.4044.138, chrome: {chromedriverVersion: 80.0.3987.106 (f68069574609..., userDataDir: C:\Users\Ravindra\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:54230}, 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}
Session ID: b5a9fe1bdf075b210b84066d6246d456
    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.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    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.getText(RemoteWebElement.java:166)
    at com.RDK.qa.testcases.SampleTestRDK.main(SampleTestRDK.java:94)

以下是HTML DOM

<ul class="select2-results__options" role="listbox" id="select2-religion-results" aria-expanded="true" aria-hidden="false"><li class="select2-results__option" role="option" aria-disabled="true" data-select2-id="799">Select Religion</li><li class="select2-results__option" id="select2-religion-result-m0f4-Hindu" role="option" aria-selected="true" data-select2-id="select2-religion-result-m0f4-Hindu">Hindu</li><li class="select2-results__option" id="select2-religion-result-wjht-Muslim - Shia" role="option" aria-selected="false" data-select2-id="select2-religion-result-wjht-Muslim - Shia">Muslim - Shia</li><li class="select2-results__option" id="select2-religion-result-b75a-Muslim - Sunni" role="option" aria-selected="false" data-select2-id="select2-religion-result-b75a-Muslim - Sunni">Muslim - Sunni</li><li class="select2-results__option" id="select2-religion-result-m5xr-Muslim - Others" role="option" aria-selected="false" data-select2-id="select2-religion-result-m5xr-Muslim - Others">Muslim - Others</li><li class="select2-results__option select2-results__option--highlighted" id="select2-religion-result-2s26-Christian" role="option" aria-selected="false" data-select2-id="select2-religion-result-2s26-Christian">Christian</li><li class="select2-results__option" id="select2-religion-result-osni-Sikh" role="option" aria-selected="false" data-select2-id="select2-religion-result-osni-Sikh">Sikh</li><li class="select2-results__option" id="select2-religion-result-np6g-Jain - Digambar" role="option" aria-selected="false" data-select2-id="select2-religion-result-np6g-Jain - Digambar">Jain - Digambar</li><li class="select2-results__option" id="select2-religion-result-a2ie-Jain - Shwetambar" role="option" aria-selected="false" data-select2-id="select2-religion-result-a2ie-Jain - Shwetambar">Jain - Shwetambar</li><li class="select2-results__option" id="select2-religion-result-ugt0-Jain - Others" role="option" aria-selected="false" data-select2-id="select2-religion-result-ugt0-Jain - Others">Jain - Others</li><li class="select2-results__option" id="select2-religion-result-pbl6-Parsi" role="option" aria-selected="false" data-select2-id="select2-religion-result-pbl6-Parsi">Parsi</li><li class="select2-results__option" id="select2-religion-result-4jon-Buddhist" role="option" aria-selected="false" data-select2-id="select2-religion-result-4jon-Buddhist">Buddhist</li><li class="select2-results__option" id="select2-religion-result-y89i-Inter-Religion" role="option" aria-selected="false" data-select2-id="select2-religion-result-y89i-Inter-Religion">Inter-Religion</li></ul>
评论