WebDriver无法打开URL

时间:2011-12-20 15:10:13

标签: selenium webdriver testng

我对selenium很新,所以我无法用我的代码发现问题。我正在使用webDriver支持的selenium对象,它启动驱动程序,但从不打开URL,驱动程序稍后关闭。这次发生在我身上的原因仅仅是因为我从URL中删除了“http”。那么这次造成了什么呢?

public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login"); 
System.out.println(selenium.getXpathCount("//img"));
}

设置如下:

public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
    Thread.sleep(2000);
}

拆解方法只包含driver.close()。 我正在使用selenium 2.14和testNG Eclipse插件。

2 个答案:

答案 0 :(得分:1)

您可能需要执行以下操作

selenium.open("www.testsite.com/login");

从selenium网站查看此示例:

// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();

// A "base url", used by selenium to resolve relative URLs
 String baseUrl = "http://www.google.com";

// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");

// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();

//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();

link to selenium

答案 1 :(得分:0)

您需要像下面一样添加driver.get(url)。

public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}