gpt4 book ai didi

java - ChromeDriver 无法访问选择

转载 作者:行者123 更新时间:2023-12-01 15:35:22 30 4
gpt4 key购买 nike

我正在尝试强制 ChromeDriver 从选择框中选择一个项目。然而它失败了

    Options = select.getOptions(); 

奇怪的是,在下面的代码中,getText();方法打印出选择中的所有项目:

  productChoooser.getText() = ...
BASE 2013
BASE 2014

但我仍然遇到异常...尝试单击选择框中的项目的代码:

    List<WebElement> Options;
WebElement productChoooser = driver.findElement(By.id("mainForm:aucPanelId:1:product"));
System.out.println("productChoooser.getText() = " + productChoooser.getText());
Select select = new Select(productChoooser);
Options = select.getOptions();
String product = "BASE 2013";
for (WebElement option:Options){
System.out.println("option.getText() = " + option.getText());
if(option.getText().equals(product)){
option.click();
}
}

有人知道如何避免吗?我尝试将 Chrome 和 chromedriver.exe 更新到最新版本,但没有成功...

堆栈跟踪:

org.openqa.selenium.WebDriverException: (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 47 milliseconds Build info: version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_20' Driver info: driver.version: RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:435) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:231) at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:161) at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:227) at org.openqa.selenium.By$ByTagName.findElements(By.java:313) at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:146) at org.openqa.selenium.support.ui.Select.getOptions(Select.java:70) at com.deutscheboerse.testing.RegulationTest.selectProduct(RegulationTest.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

最佳答案

        WebElement select = webDriver.findElement(selector);
List<WebElement> optionList = select.findElements(By.tagName("option"));
for (WebElement option : optionList) {
if (option.getText().trim().equalsIgnoreCase("your value")) {
option.click();
break;
}
}

关于java - ChromeDriver 无法访问选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944039/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com