尝试使用Selenium Web Driver
在
Rahul Shetty Academy网站上选择出发城市时,未选择使用
XPath
选择的出发城市。我的代码位于星号下方,我在星号内收到以下错误。
例外:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@selectedvalue='BLR']"}
(Session info: chrome=121.0.6167.85)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.17.0', revision: 'e52b1be057*'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '14.2.1', java.version: '17.0.9'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [f9e14578261c798b2021551ffe86d360, findElement {value=//input[@selectedvalue='BLR'], using=xpath}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 121.0.6167.85, chrome: {chromedriverVersion: 121.0.6167.85 (3f98d690ad7e..., userDataDir: /var/folders/md/pyls622d4kd...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:61894}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: mac, proxy: Proxy(), se:cdp: ws://localhost:61894/devtoo..., se:cdpVersion: 121.0.6167.85, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: f9e14578261c798b2021551ffe86d360
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:134)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:519)
at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
at DynamicDropdowns.main(DynamicDropdowns.java:19)
DynamicsDropdown 类:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DynamicDropdowns {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "/Users/mfigures/Documents/COFTA/chromedriver-mac-x64/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
//input[@selectedvalue="BLR"]
//input[@selectedvalue="MAA"]
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//input[@selectedvalue='BLR']")).click();
driver.findElement(By.xpath("//input[@selectedvalue='MAA']")).click();
}
}
您的下拉容器将在单击后一段时间后呈现。 您需要添加等待渲染条件。
要正确处理这种情况,您需要等待下拉选项出现,然后您需要单击它,等待它不可见,再次获取下拉选项并过滤可见选项。
WebDriverWait wdwait = new WebDriverWait(driver, 10);
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
wdwait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("ctl00_mainContent_ddl_originStation1_CTXT")))
.get(0)
.click();
WebElement from = wdwait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#dropdownGroup1 [value=BLR]")));
from.click();
wdwait.until(ExpectedConditions.invisibilityOf(from));
WebElement to = wdwait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#dropdownGroup1 [value=MAA]")))
.stream()
.filter(WebElement::isDisplayed)
.findFirst()
.get();
to.click();