java - 从 <li> 中选择下拉值

标签 java eclipse selenium webdriver

我有一个下拉菜单,其中包含许多选项,我必须在其中选择一个并单击它们,为此我使用下面的代码,但它给了我以下错误:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

下面是我编写的代码。

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>

最佳答案

如果在创建元素后 DOM 发生更改,并且该元素已从 DOM 中删除或不再存在于 DOM 中,通常会出现过时元素。

因此,在您的场景中,您似乎最初创建了 listReligion ,然后搜索了 Regliion 。然后,只有与您的搜索匹配的宗教才会显示在用户界面中。因此,某些元素将被删除,这可能是导致过时元素异常的原因。

将列表 listReligions 行放在 SendKeys 下方并尝试运行它。

尝试以下几行。

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

关于java - 从 <li> 中选择下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61850750/

相关文章:

java - Android - 计算服务器目录中的文件数

java - 如何返回 DocumentSnapShot 作为方法的结果?

java - 如何从专业开发人员的 Java IDE (IntelliJ IDEA) 运行 SpringBoot 应用程序

python - 如何在Python中使用selenium在不同WebDriver打开的不同chrome浏览器窗口之间切换?

java - 在 jenkins 中使用 Docker 从站时,Selenium 驱动程序崩溃并给出 NoSuchSessionException

java - 元素无法使用文本-selenium webdriver 定位

java - 使用 Hibernate 更新两个不相关的表,如果任何事务失败,我们应该回滚这两个事务吗?

java - 验证旧 CSV 文件中的信息后打印写入新 CSV 文件并将 DATA INFILE 加载到 MySQL 中

java -\Java\jdk1.6.0_03\bin 这个时候出乎意料

java - 如何手动将项目添加到另一个项目的构建路径?