当前位置: 首页 > 知识库问答 >
问题:

从中选择下拉值

邢华清
2023-03-14
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>

共有1个答案

越飞鸾
2023-03-14

如果在您创建了元素之后DOM发生了更改,如果该元素已经从DOM中删除,或者它不再存在于DOM中,则通常会发生陈旧元素。

因此,在您的场景中,看起来您最初创建了listReligion,然后搜索Reglion。然后只有与您的搜索匹配的宗教才会显示在UI中。因此,一些元素将被删除,这可能是导致陈旧元素异常的原因。

将list listReligions行放置在SendKeys下面并尝试运行它。

    driver.findElement(By.cssSelector(".select2-search__field")).sendKeys(MyReligion);
List <WebElement> listReligion = driver.findElements(By.cssSelector("#select2-religion-results>li"));
 类似资料:
  • 我是硒的新手,我试图从下拉列表中选择一个选项。下拉列表的超文本标记语言如下: WebElement的是: 我已经尝试了几乎所有的方法,我可以在互联网上找到,但没有任何效果。我试图使用类,包装了,但它抛出了一个异常。 我试图丢失列表中的所有选项,但在这种情况下得到了异常应该有标签,但它有。我需要使用作为findelements的标识符。 请帮我解决这个问题。

  • 我正在尝试从下拉列表中选择一个选项,该选项在单击定位器之前不会填充。这是我在Firebug中看到的: 到目前为止,我拥有的代码是: 我得到一个意外的TagNameException:元素应该是“选择”,但是“div”。我不知道如何处理这个,因为我以前只使用过选择。 假设我想为代理代码选择“523-23-20275”。我该怎么做? 感谢您的帮助!谢谢

  • 问题内容: 我正在建立一个网站来学习编码,并且正在尝试建立一个工具,用户单击一个选择/下拉列表,其中包含从数据库 cat 提取的某些类别名称,然后另一个选择与从数据库 subcat 提取的子类别名称一起出现。这几乎与Yelp的(向下分类)完全一样,就像Yelp的(向下分类)一样。 我还做了一个图: 我已经有一个从 cat 数据库中提取的类别下拉列表: 我有一个从subcat数据库中提取的subca

  • 问题内容: 我想从下拉选项中选择一个值。html如下: 我尝试如下: 怎么了 请帮我! 问题答案: 阿德里安Ratnapala是正确的,也是我会选择过,所以你可以尝试以下方法: 要么 要么 您可以使用: 单击此处以获取更多信息。

  • 问题内容: 我想创建一个下拉菜单,其中包含图像而不是文本作为选项。我在此处进行了一些Google搜索,然后在Stack Overflow上进行了搜索,通常给出的答案是使用jQuerycombobox。 在我看来,这种解决方案的问题在于您必须提供文本。看起来图像只是左侧文字的图标。如果我错了,请纠正我,但是此解决方案将无法解决我要尝试的工作- 完全用图像替换文本。 我正在尝试做的事情的一些背景知识-

  • 我正在创建一个演示站点的实践测试,然而,我在从下拉列表中选择一个值时遇到了一个问题,我无法找到元素,然而,它是正确的ID,我也尝试了ID和CSS选择器,但没有运气:(我将在下面发布HTML和Selenium代码: HTML Ruby代码: