使用下面的简单测试代码,但selenium总是喜欢选择第二个谷歌建议结果作为搜索文本:
例如:
我输入“Selenium”,谷歌会给出如下建议清单:
硒
Selenium WebDriver
然后webdriver将始终选择“Selenium WebDriver”。但我使用webdriver将Keys发送为“Selenium”。
这是webdriver的错误吗?
public class HelloWorld {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(caps);
driver.get("http://www.google.com");
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
//Thread.sleep(5000);
driver.findElement(By.name("btnK")).submit();
}
}
答案 0 :(得分:0)
我很确定googles立刻的结果会让你烦恼。在您输入查询的中途后,Google将开始向您显示结果,并且“btnK”按钮将不再可见。试试这个:
@Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
inputField.sendKeys(Keys.ENTER);
}