尝试将 Selenium 与 SafariWebDriver 一起使用。使用 jars selenium-server-standalone-2.33.0.jar - 运行 Jetty 服务器
java -jar ./lib/selenium-server-standalone-2.33.0.jar
selenium-java-2.33.0.jar (我没有使用 Maven 来设置项目 - 只是下载了 jars,并用 javac 编译)
javac -s ./src -cp ./classes:./lib/selenium-java-2.33.0.jar:./lib/selenium-server-standalone-2.33.0.jar ./src/jgf/Selenium2Example.java -d ./classes
编写了一个类,它或多或少是 Selenium2Example 的复制/粘贴,但使用 SafariWebDriver 而不是 FirefoxWebDriver
但是当代码执行时,我在 Safari Web 浏览器中收到消息(使用 Snow Leopard 和 Safari 5.1.9 (6534.59.8))。
无法与 SafariDriver 建立连接
关于如何解决这个问题的任何想法?
顺便说一句:我没有通过注册为 Apple 开发人员使用从源代码编译的带有证书的 Safari 扩展 - 我认为这适用于早期的 jar 版本。
这是代码
package jgf;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example {
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();
WebDriver driver = new SafariDriver();
// 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();
}
}