如何从下拉菜单中选择项目



如何从下拉菜单中选择项目?

这是错误提示警告:无法找到要使用的CDP版本。您可能需要使用类似于org.seleniumhq.selenium:selenium-devtools-v86:4.0.0的东西来包含对特定版本的CDP的依赖,其中版本("v86")与您正在使用的基于chromium的浏览器的版本匹配,并且工件的版本号与Selenium的版本号相同。线程"main"中的异常org. openqa.seleniem . nosuchelementexception: no such element:无法定位元素:{"method";xpath","selector";//[@id="select2- massbend-result-mnkp - 1739aded802119fafe3ae2cda03e0075 "]"}(会话信息:chrome=102.0.5005.115)命令:[37875cebf5ae802a1b5455859349376c, findElement {using=xpath, value=//[@id="select2- massbend- result-mnkp- 1739aded802119fafe3ae2cda03e0075 "]}]Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 102.0.5005.115, chrome: {chromedriverVersion: 102.0.5005.61 (0e59bcc00cc4… users OMAR~1.ELSAppData…}, goog:chromeOptions: {debuggerAddress: localhost:64998}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: proxy (), se:cdp: ws://localhost:64998/devtoo…, se:cdpVersion: 102.0.5005.115, setWindowRect: true, strictFileInteractability: false, timeout: {implicit: 0, pagload: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}会话ID: 37875cebf5ae802a1b5455859349376c

浏览器打开并登录下拉菜单被按下,但它不会选择其中的任何元素

<span class="select2-container select2-container--default select2-container--open" style="position: absolute; top: 207.953px; left: 80px;"><span class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 501px;"><span class="select2-search select2-search--dropdown"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox"></span><span class="select2-results"><ul class="select2-results__options" role="tree" id="select2-massbending-results" aria-expanded="true" aria-hidden="false"><li class="select2-results__option" role="treeitem" aria-selected="true">select</li><li class="select2-results__option" id="select2-massbending-result-qv5z-E17969A3569FFFB9FE38C4DA499D6B7C" role="treeitem" aria-selected="false">الاهلى للخدمات الطبية</li><li class="select2-results__option" id="select2-massbending-result-09vf-3635DD9DC6F14B9F7531A4B4AF17C4F8" role="treeitem" aria-selected="false">EGYCare</li><li class="select2-results__option select2-results__option--highlighted" id="select2-massbending-result-2pqn-E459950A60D539CFA196AA887ABF13F1" role="treeitem" aria-selected="false">Seif Health Care</li><li class="select2-results__option" id="select2-massbending-result-z8ev-C7D55B7B6FCA0FCE1640BC9FE634523E" role="treeitem" aria-selected="false">Prime Health Egypt</li><li class="select2-results__option" id="select2-massbending-result-d4gu-DB1E9157DE264E4CAF25D2B872C926B7" role="treeitem" aria-selected="false">Ciaf Health Care</li><li class="select2-results__option" id="select2-massbending-result-oh0b-638D4A97AE9CB54A0259EB0FF40D7A5F" role="treeitem" aria-selected="false">Future</li><li class="select2-results__option" id="select2-massbending-result-8et4-8B9348FE31D8C944EC8D18F5C94E0608" role="treeitem" aria-selected="false">MedSure</li><li class="select2-results__option" id="select2-massbending-result-vwyx-06E8A65DC99874AAC26EB392F7A9AC31" role="treeitem" aria-selected="false">Queuing test E</li><li class="select2-results__option" id="select2-massbending-result-4cqc-A350E8628BE44651F86CBF3B45B8ADCD" role="treeitem" aria-selected="false">Misr Healthcare</li><li class="select2-results__option" id="select2-massbending-result-6dky-AB940187B0B10A0838E133BA0E3A7F05" role="treeitem" aria-selected="false">INAYA EGYPT</li><li class="select2-results__option" id="select2-massbending-result-gtoo-E7B06E1096A9AE53900CDB15DE476F92" role="treeitem" aria-selected="false">Belshifa</li></ul></span></span></span>

代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Nicedeer {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
//Open Nicedeer
driver.get("http://www.nicedeer.net/");
//Enter Username
WebElement element = driver.findElement(By.xpath("//*[@id="UserName"]"));
element.sendKeys("xxxx");
//Enter Password
WebElement elementPass = driver.findElement(By.xpath("//*[@id="Password"]"));
elementPass.sendKeys("xxx");
//Login to account
WebElement button = driver.findElement(By.xpath("//*[@id="loginBTN"]"));
button.click();
//Choose Payer
WebElement payer = driver.findElement(By.xpath("//*[@id="select2-massbending-container"]"));
payer.click();
//Select Payer
driver.findElement(By.xpath("//*[@id="select2-massbending-result-mnkp-1739ADED802119FAFE3AE2CDA03E0075"]")).click();
}
}

您可以通过下面的操作从这个下拉列表中选择值:

-首先点击下拉元素,如:

driver.findElement(By.id("correct_id")).click();

-要选择

的元素的第二个click
driver.findElement(By.xpath("xpath")).click();

您可以在xpath下面使用下拉元素,例如,用于Future

- //li[contains(text(),'Future')]

- //*[normalize-space()='Future']