英文:
Unable to select option from drop down(Tried all ways) in selenium
问题
我正在尝试从下拉菜单中选择选项。 但是它没有被选中。 测试用例在不出现任何错误且未选择选项的情况下通过。 由于它是HTML下拉菜单,我已经使用了点击操作。 我尝试了Select类但没有奏效。 网站是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();
恳请您的帮助。 谢谢
英文:
I am trying to select the option from the drop down menu. But it is not getting selected. The test case passes without any error and without selecting the option. Since it is HTML drop down I have used click. I tried Select class but it did not work. The site is https://demoqa.com/automation-practice-form/
The code I have written here is
> 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();
your help is solicited. Thanks
答案1
得分: 1
不同的元素,您需要在发送按键后点击,如“Uttar Pradesh”
使用以下代码:
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();
英文:
Its different element u need to click after sending keys as "Uttar Pradesh"
Use below code
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();
答案2
得分: 0
以下代码适用于我。
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");
英文:
The code below worked for me.
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论