无法从selenium中的下拉列表中选择选项(尝试了所有方法)

bpsygsoo  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(423)

我正在尝试从下拉菜单中选择该选项。但它没有被选中。测试用例通过时没有任何错误,也没有选择选项。因为它是html下拉列表,所以我使用了单击。我试着选了一门课,但没用。网站是https://demoqa.com/automation-practice-form/ 我在这里写的代码是

>     JavascriptExecutor js=(JavascriptExecutor)driver;
>     Actions act=new Actions(driver);
>     js.executeScript("window.scrollBy(0,500)");
>     WebElement we=driver.findElement(By.xpath("//div[@id='state']"));
>     act.moveToElement(we).click().build().perform();
>     WebElement we3=driver.findElement(By.xpath("//div[contains(.,'Uttar
> Pradesh')]/following-sibling::div/descendant::input"));
        act.moveToElement(we3).click(we3).build().perform();

我们请求你的帮助。谢谢

ruyhziif

ruyhziif1#

它的不同元素你需要点击后,发送键为“北方邦”
使用以下代码

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

xoshrz7s2#

下面的代码对我有用。

WebDriver Driver = new ChromeDriver();
    Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    String url = "https://demoqa.com/automation-practice-form";
    Driver.get(url);
    WebElement products=Driver.findElement(By.xpath("//input[@id='react-select-3-input']"));
    products.sendKeys("Uttar Pradesh");
    products.sendKeys(Keys.ARROW_DOWN);
    products.sendKeys(Keys.ENTER);
    System.out.println("completed");

相关问题