开发者问题收集

无法在 Selenium 中选择下拉值

2020-08-10
1310

我尝试在 https://demoqa.com/automation-practice-form 表单中为 州和城市 字段选择一个值。当我尝试访问州字段时,它会抛出错误 没有这样的元素:无法找到元素

以下是以下代码片段和错误消息。

//State
new WebDriverWait(driver , 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-3-input']"))).sendKeys("NCR");
new WebDriverWait(driver ,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class,'css-2613qy-menu')]"))).click();

//City
new WebDriverWait(driver , 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-4-input']"))).sendKeys("Noida");
new WebDriverWait(driver ,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class,'css-2613qy-menu')]"))).click();

错误

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //div[contains(@class,'css-2613qy-menu')] (tried for 20 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at newpackage.Example.main(Example.java:117)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@class,'css-2613qy-menu')]"}
  (Session info: chrome=84.0.4147.105)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'DESKTOP-E926NDJ', ip: '192.168.178.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 84.0.4147.105, chrome: {chromedriverVersion: 84.0.4147.30 (48b3e868b4cc0..., userDataDir: C:\Users\hp\AppData\Local\T...}, goog:chromeOptions: {debuggerAddress: localhost:54539}, 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, webauthn:virtualAuthenticators: true}
Session ID: c4cdf6fd8a060246b9284b9e4be7ccf7
*** Element info: {Using=xpath, value=//div[contains(@class,'css-2613qy-menu')]}
    at sun.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    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.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:641)
    at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:638)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    ... 1 more

还建议使用代码来选择城市字段。

3个回答

您尝试点击的元素位于不同的 div 中。 在下面的代码片段中使用 xpath。

或者,您也可以使用类名

 //State
 new WebDriverWait(driver , 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-3-input']"))).sendKeys("NCR");
 new WebDriverWait(driver ,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@id,'react-select')]"))).click();

//City
new WebDriverWait(driver , 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-4-input']"))).sendKeys("Noida");
new WebDriverWait(driver ,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@id,'react-select')]"))).click();
Ankit Agrawal
2020-08-10

感谢提供选择“州”和“城市”的代码,这真的很有帮助。我也对代码做了一些小改动,以下代码也有效。

WebElement e1 = dr.findElement(By.xpath("//input[@id='react-select-3-input']"));
e1.sendKeys("Uttar Pradesh");
e1.sendKeys(Keys.ENTER);

WebElement e2 = dr.findElement(By.xpath("//input[@id='react-select-4-input']"));
e2.sendKeys("Lucknow");
e2.sendKeys(Keys.ENTER);
user14132305
2020-08-19

您可以使用以下代码片段单击“州”和“城市”,然后从下拉菜单中选择适当的值:

   // To Click State drop down
    driver.findElement(By.id("state")).click();

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1
            .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-3-input']")));
    element1.sendKeys("NCR");
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    // To Click City drop down
    driver.findElement(By.id("city")).click();

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    WebElement element2 = wait2
            .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='react-select-4-input']")));
    element2.sendKeys("Delhi");
    Robot robot1 = new Robot();
    robot1.keyPress(KeyEvent.VK_ENTER);
    robot1.keyRelease(KeyEvent.VK_ENTER);
Bhavesh Soni
2020-08-10