如何使用Selenium和Java从动态下拉菜单中选择自动建议。

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

How to select the auto suggestion from the dynamic dropdown using Selenium and Java

问题

我正在尝试在以下表单中选择Subjects字段的值:https://demoqa.com/automation-practice-form

这是一个根据我们的输入动态提供建议的输入字段,稍后我们需要从这些建议中选择值。我无法选择所需的值。

下面的代码只填充了输入区域,但没有选择值。

driver.findElement(By.id("subjectsInput")).sendKeys("English");
driver.findElement(By.id("subjectsInput")).click(); //这行不会点击所需的值。

如何选择所需的值。请帮忙。

英文:

I am trying to select the value for Subjects field in the following form: https://demoqa.com/automation-practice-form

It is an input field that dynamically gives suggestions based on our input and later we need to select values from those suggestions. I am unable to select the desired value.

The Below code only populates the input area but the value is not selected.

driver.findElement(By.id("subjectsInput")).sendKeys("English");
driver.findElement(By.id("subjectsInput")).click(); //This line doesnot click on the desired value.

How to Select the desired value. Please help.

答案1

得分: 2

以下是已翻译的代码部分:

WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Driver.manage().window().maximize();
String url = "https://demoqa.com/automation-practice-form";
Driver.get(url);
WebElement products=Driver.findElement(By.id("subjectsInput"));
products.sendKeys("English");
products.sendKeys(Keys.ARROW_DOWN);
products.sendKeys(Keys.ENTER);

请注意,我已经将HTML实体代码(如")还原为原始文本。

英文:

The code below worked for me.

	WebDriver Driver = new ChromeDriver();
	Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	//Driver.manage().window().maximize();
	String url = "https://demoqa.com/automation-practice-form";
	Driver.get(url);
	WebElement products=Driver.findElement(By.id("subjectsInput"));
	products.sendKeys("English");
	products.sendKeys(Keys.ARROW_DOWN);
	products.sendKeys(Keys.ENTER);

答案2

得分: 0

要调用仅限于_auto建议_ English 的操作,您需要使用WebDriverWait来等待 elementToBeClickable(),并且您可以使用以下任一定位策略

  • css选择器

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#subjectsInput"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.subjects-auto-complete__menu"))).click();
    
  • xpath

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='subjectsInput']"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'subjects-auto-complete__menu')]"))).click();
    
  • 浏览器快照:

如何使用Selenium和Java从动态下拉菜单中选择自动建议。

英文:

To invoke click on the only auto suggestion English you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#subjectsInput"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.subjects-auto-complete__menu"))).click();
    
  • xpath:

    driver.get("https://demoqa.com/automation-practice-form");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='subjectsInput']"))).sendKeys("English");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'subjects-auto-complete__menu')]"))).click();
    
  • Browser Snapshot:

如何使用Selenium和Java从动态下拉菜单中选择自动建议。

huangapple
  • 本文由 发表于 2020年8月7日 20:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63301508.html
匿名

发表评论

匿名网友

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

确定