Java Selenium在浏览外部URL时尝试连接到本地主机
我正在使用Selenium v2.30.0和Firefox 19.0。当我执行以下代码时:Java Selenium在浏览外部URL时尝试连接到本地主机,java,selenium,Java,Selenium,我正在使用Selenium v2.30.0和Firefox 19.0。当我执行以下代码时: public class First_Example { public static void main(String[] args) { // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface,
public class First_Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
公共类优先\u示例{
公共静态void main(字符串[]args){
//创建Firefox驱动程序的新实例
//请注意,代码的其余部分依赖于接口,
//而不是执行。
WebDriver=newfirefoxdriver();
//现在用这个来访问谷歌
驱动程序。获取(“http://www.google.com");
//或者也可以这样做
//驱动程序。导航()。到(“http://www.google.com");
//按名称查找文本输入元素
WebElement=driver.findElement(By.name(“q”));
//输入要搜索的内容
元素。sendKeys(“Cheese!”);
//现在提交表单。WebDriver将从元素中为我们找到表单
元素。提交();
//检查页面的标题
System.out.println(“页面标题为:“+driver.getTitle());
//谷歌的搜索是用JavaScript动态呈现的。
//等待页面加载,10秒后超时
(新的WebDriverWait(驱动程序,10))。直到(新的预期条件(){
公共布尔应用(WebDriver d){
返回d.getTitle().toLowerCase().startsWith(“cheese!”);
}
});
//应该看到:“奶酪!-谷歌搜索”
System.out.println(“页面标题为:“+driver.getTitle());
//关闭浏览器
driver.quit();
}
}
我得到一个错误,说:
“45000毫秒后无法连接到端口7055上的主机本地主机”
我到处寻找答案,但没有成功。感谢您的帮助。在等待回复时似乎遇到了一些问题 请尝试以下代码:
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("Cheers!");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
WebElement resultsDiv = driver.findElement(By.className("gssb_e"));
// If results have been returned, the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
// And now list the suggestions
List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));
for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
publicstaticvoidmain(字符串[]args)
{
WebDriver=newfirefoxdriver();
驱动程序。获取(“http://www.google.com/");
WebElement query=driver.findElement(By.name(“q”));
sendKeys(“干杯!”);
//睡眠直到我们想要的div可见或5秒结束
长端=System.currentTimeMillis()+5000;
while(System.currentTimeMillis()
您需要将Selenium更新到v2.31。Firefox 19在任何更低版本中都不受支持
您是否尝试过替代的
驱动程序。导航()到(“http://www.google.com");代码>?