1.浏览器列表:
*firefox *mock *firefoxproxy *pifirefox *chrome *iexploreproxy *iexplore *firefox3 *safariproxy
*googlechrome *konqueror *firefox2 *safari *piiexplore *firefoxchrome *opera *webdriver *iehta *custom
2. loginForm中查找username与password(https://gist.github.com/1047207/c670c7d1fe4a5a60d361d66869b20cc2ab1a4bf4)
elem1=br.find_element_by_xpath("//form[@id='loginForm']/input[1]")
elem1.send_keys('username')
elem2=br.find_element_by_xpath("//form[@id='loginForm']/input[2]")
elem2.send_keys('password')
3.Java中调用Firefox,Chrome与 IE
driver = new FirefoxDriver(); //调用Firefox
driver = new ChromeDriver();
driver = new InternetExplorerDriver();
调用IE浏览器的时候会出现错误,“首先 需要把IE的保护模式关闭,将IE的“Protected Mode”设置取消。Internet Options->Security->" Internet " icon->Enable Protected Mode,或者添加代码如下:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities);
然后 设置 System.setProperty("webdriver.ie.driver","E:\\seleniumIDE\\IEDriverServer.exe");
4. driver.get("http://www.google.com.hk");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("hello Selenium!");
element.submit();
5.查看元素的xpath
首席在firefox上安装firebug,然后highlight元素,点击右键选择“使用firebug查看元素”,然后在选中的代码行点击右键,选择“复制xpath路径”就可以了。
6. Selenium java 方法
driver.findElement(By.id(<element ID>))
driver.findElement(By.Name(<element name>))
driver.findElement(By.className(<element class>))
driver.findElement(By.tagName(<htmltagname>))
driver.findElement(By.linkText(<linktext>))
driver.findElement(By.PartialLinkText(<linktext>))
driver.findElement(By.cssSelector(<cssselector>))
driver.findElement(By.xpath(<xpath query expression>))
7. List<WebElement> userNames=driver.findElements(By.xpath("//input")); 需要导入包:import java.util.List;
8. FAILED CONFIGURATION: @AfterClass tearDown 这在testng中怎么解决呢? 答:把driver.quit() 改为driver.close()就可以了
9. 对浏览器进行前进、后退操作
driver.navigate().forward();
driver.navigate().back();