如何单击自动建议输入文本字段中的选项? Selenium WebDriver

时间:2013-03-17 05:16:53

标签: java selenium

我正在使用Selenium WebDriver

运行测试

输入文本字段(“选择来源:按名称:”)时出现问题。 当我在文本字段中输入字符串“ABC Premium News(Australia)”时,会出现一个选项。然后我需要点击(或选择)它。我尝试了所有方法w / 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...

1 个答案:

答案 0 :(得分:2)

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

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();