【问题标题】:How to click an option in an auto-suggest input textfield? Selenium WebDriver如何单击自动建议输入文本字段中的选项? Selenium 网络驱动程序
【发布时间】:2013-03-19 06:50:24
【问题描述】:

我正在使用 Selenium WebDriver 运行测试

我的输入文本字段有问题 ("Select Source: By Name:")。 当我在文本字段中输入字符串“ABC 高级新闻(澳大利亚)”时,会出现一个选项。然后我需要单击(或选择)它。我尝试了所有带有 fireEvent 的方法...没有用。

以下是源码:

driver.get("http://www..."); 
driver.switchTo().frame("mainFrame");
WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));
sourceTitle.sendKeys("ABC Premium News (Australia)"); 
//Now a "combobox-like" option "ABC Premium News (Australia)" shows up...how do I click it?

// I tried fireEvent...it did not help. The following is one of my trials that does not work:
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www....");
sel.type("//input[@id='destination']", "ABC Premium News (Australia)");
sel.fireEvent("//input[@id='destination']", "keydown");
// In addition to keydown, I tried: onclick, onfocus, onblur...

【问题讨论】:

  • 我很抱歉@Amine Hajyoussef。我又试了一次,似乎这种方法只有助于将值输入到文本文件中,但是,它没有帮助选择“组合框 - 类似”选项。任何人,请运行上述代码行或访问网站查看文本字段。
  • 你能给我们看看html和截图吗?

标签: java selenium


【解决方案1】:

我知道这不是最好的方法,但它仍然可以用作临时解决方法。

WebElement sourceTitle = driver.findElement(By.name("sourceTitle"));

WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small"));

sourceTitle.sendKeys("ABC Premium News (Australia)"); 

Thread.sleep(5000);

Actions actions = new Actions(driver);

actions.click(small).perform();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多