java 如何使用 Selenium 填写自动完成输入框?(为什么自动输入不会加载自动完成选项,而手动输入会加载?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17741060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:50:56  来源:igfitidea点击:

How to fill in an autocomplete inputbox using Selenium? (Why an automated input does not load autocomplete options BUT a manual input does?)

javaseleniumautocompleteinputbox

提问by Buras

The following code tests an autocomlete box of a webpage:

以下代码测试网页的自动完成框:

public class Test {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","chromedriver\chromedriver.exe");     
        WebDriver driver = new ChromeDriver();
        driver.get("http://www..............com"); 
        driver.switchTo().frame("mainFrame");

        WebDriverWait waitst = new WebDriverWait(driver, 120);
        waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));

        WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
        WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small"));
        sourceTitle.sendKeys("Times"); 
        Thread.sleep(5000);
        Actions actions = new Actions(driver);
        actions.click(small).perform();

    }

}

Why doesn't the autosuggest box load? IMPORTANT:try to type in "..........."manually ... the autocomplete box will load perfectly fine!!! So, why does not cssSelectorwork, why doesn't it load the autocomplete box?

为什么自动提示框不加载?重要提示:尝试手动输入“…………”……自动完成框将完美加载!!!那么,为什么不起作用cssSelector,为什么不加载自动完成框?

How come an automated input does not allow for autocomplete options BUT a manual input does???

为什么自动输入不允许自动完成选项但手动输入允许???

PS: I also tried fireEvent, sendKeysbut nothing works.

PS:我也试过fireEventsendKeys但没有任何效果。

采纳答案by joostschouten

I tried your code, it does exactly what the manual walkthough does. "Associated Press, The" returns only a "No Match, please try sources". In your code you then try to click on the next form list item, not the results popup. The autosuggest popup is dynamically populated at the top of your html page positioned under the input form. The following code does select the first option on your drop down.

我试过你的代码,它完全符合手动演练的作用。“Associated Press, The”仅返回“不匹配,请尝试来源”。在您的代码中,您然后尝试单击下一个表单列表项,而不是结果弹出窗口。自动建议弹出窗口动态填充在位于输入表单下方的 html 页面顶部。以下代码确实选择了下拉列表中的第一个选项。

@Test
public void test() throws InterruptedException {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.lexisnexis.com/hottopics/lnacademic/?verb=sf&sfi=AC00NBGenSrch"); 
        driver.switchTo().frame("mainFrame");

        WebDriverWait waitst = new WebDriverWait(driver, 0);
        waitst.until(ExpectedConditions.visibilityOfElementLocated(By.name("sourceTitle")));

        WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
        sourceTitle.sendKeys("Times"); 
        Thread.sleep(5000);
        WebElement firstItem = driver.findElement(By.xpath("//*[@class='auto_suggest']/*[@class='title_item']"));
        firstItem.click();
}

回答by TesterFromA3rdWorldCountry

I found a workaround about this. My problem was:

我找到了一个解决方法。我的问题是:

  1. Selenium inputted 'Mandaluyong' to an auto-suggest location field
  2. The auto-suggest field appears together with the matched option
  3. Then selenium left the auto-suggest drop-down open not selecting the matched option.
  1. Selenium 将“Mandaluyong”输入到自动建议位置字段
  2. 自动建议字段与匹配的选项一起出现
  3. 然后 selenium 使自动建议下拉菜单保持打开状态,没有选择匹配的选项。

What I did was:

我所做的是:

        driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
        driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);

This is because on a manual test, when I try to press TAB key, two things were done by the system:

这是因为在手动测试中,当我尝试按 TAB 键时,系统做了两件事:

  1. Picks the matched option from the auto-suggest drop-down
  2. Closes the auto-suggest drop-down
  1. 从自动建议下拉菜单中选择匹配的选项
  2. 关闭自动建议下拉菜单

Hope this helps.

希望这可以帮助。

回答by nikil

// allow autopuplation value to fill the text box.

// 允许自动填充值填充文本框。

// wait for 6 sec make sure auto value is inserted thread.sleep(6000L);

// 等待 6 秒,确保插入了自动值 thread.sleep(6000L);

// clear auto filled value driver.findElement(By.name("txtBox")).clear();

// 清除自动填充的值 driver.findElement(By.name("txtBox")).clear();

driver.findElement(By.name("txtBox")).sendKeys("value");

driver.findElement(By.name("txtBox")).sendKeys("value");

回答by Ritesh Kadam

Try first clicking on the Input textbox. This will trigger the auto populating dropdown box and then enter the required value using sendKeys

尝试首先单击输入文本框。这将触发自动填充下拉框,然后使用 sendKeys 输入所需的值