</p><p>public class Example {</p><p> public static void main(String[] args) {</p><p> // Create a new instance of the html unit driver</p><p> // Notice that the remainder of the code relies onthe interface, </p><p> // not the implementation.</p><p> WebDriver driver = new HtmlUnitDriver();</p><p>
</p><p> // And now use this to visit Google</p><p> driver.get("http://www.google.com");</p><p>
</p><p> // Find the text input element by its name</p><p> WebElement element = driver.findElement(By.name("q"));</p><p>
</p><p> // Enter something to search for</p><p> element.sendKeys("Cheese!");</p><p>
</p><p> // Now submit the form. WebDriver will find theform for us from the element</p><p> element.submit();</p><p>
</p><p> // Check the title of the page</p><p> System.out.println("Page title is: " +driver.getTitle());</p><p> }</p><p>}</p><p></p>
复制代码
如果你想使用Firefox浏览器。你只需要将WebDriver driver = new FirefoxDriver()。前提是你的Firefox被
安装在默认的位置。
操作系统
Firefox默认安装位置
Linux
firefox (found using "which")
Mac
/Applications/Firefox.app/Contents/MacOS/firefox
Windows
%PROGRAMFILES%\Mozilla Firefox\firefox.exe
如果你的FireFox没有被安装在指定的位置,你可以设置“webdriver.firefox.bin”
环境变量的值来指定它的位置。在Java中可以使用如下代码:
System.setProperty("webdriver.firefox.bin","thelocation of Firefox");