我是这里学习的新手:P 可能是一个非常基本的问题,但我只是不知道如何询问或搜索它。
所以我决定使用selenium为java中的网页制作一个爬虫,并且我注意到如果我没有足够快地终止 session 而不是卡住=(那么这是否意味着我必须经常调用.quit()并保持打开新的 WebDriver?或者有没有办法持续与网站交互?
例如:我希望开放 Google。输入“pie”并单击搜索,也许我不喜欢结果并希望搜索“apple pie”并继续这样做很长一段时间?
最佳答案
这就是我在游戏时间练习中所做的事情。您可以利用它。
String[] location = new String[] {
"Los Angeles",
"Santa Barbara",
"San Jose"
};
// Some code
@Test
public void testSelServerDiceTest() throws Exception {
for (int i = 0; i < location.length; i++) { // manually added for loop
selenium.open("/");
selenium.type("id=FREE_TEXT", "selenium RC JUnit");
selenium.type("id=WHERE", location[i].concat(" CA"));
selenium.click("xpath=//*[@id=\"searchSubmit\"]");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("Search results:"));
verifyTrue(selenium.isTextPresent("Search job title only"));
verifyEquals("JUnit", selenium.getText("css=div.undoLabel"));
verifyTrue(selenium.isTextPresent("selenium"));
verifyTrue(selenium.isTextPresent("Search results: 1 - 1 of 1"));
assertTrue(selenium.isTextPresent("Search results:"));
}
}
//Some more code
编辑
// webdriver code snippet
@Test
public void testRemoteWebDriverDiceTest() throws Exception {
for (int i = 0; i < location.length; i++) {
driver.get(baseUrl + "/");
driver.findElement(By.id("FREE_TEXT")).clear();
driver.findElement(By.id("FREE_TEXT"))
.sendKeys("selenium RC JUnit");
driver.findElement(By.id("WHERE")).clear();
driver.findElement(By.id("WHERE")).sendKeys(
location[i].concat(" CA"));
driver.findElement(By.xpath("//*[@id=\"searchSubmit\"]")).click();
try {
assertEquals("JUnit",
driver.findElement(By.cssSelector("div.undoLabel"))
.getText());
} catch (Error e) {
verificationErrors.append(e.toString());
}
}
}
关于java - Selenium WebDriver session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951754/