无法在Selenium中从下拉菜单中选择选项(尝试了所有方法)

huangapple go评论98阅读模式
英文:

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

无法在Selenium中从下拉菜单中选择选项(尝试了所有方法)

无法在Selenium中从下拉菜单中选择选项(尝试了所有方法)

答案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");

huangapple
  • 本文由 发表于 2020年8月21日 22:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63524747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定