您的位置:首页 > 产品设计 > UI/UE

Selenium webdriver-UI Element定位

2015-12-03 09:33 417 查看
转:http://blog.csdn.net/jillliang/article/details/8206402

1.创建Fixfox web driver实例

  WebDriver driver = new ForefoxDriver();

  WebDriver driver = new InternetExplorerDriver();

2.获取Web page

driver.get("http://www.google.com");

或者

driver.navigate().to("http://www.google.com");

//navigate().to和get()其实作用是一样的,但是navigate还可以进行浏览器的前进后退操作:

driver.navigate().forward();

driver.navigate.back();

3. 定位UI Element

先安装FireBug(http://getfirebug.com/), 然后用FireBug定位页面元素的值。 如下图所示,只要把firebug的箭头放到要定位的元素上,就可以得到该元素对应标签的值, 比如goole的textbox:<input id="lst-ib" class="lst lst-tbb" type="text" maxlength="2048" name="q" autocomplete="off" size="41" title="Google 搜索" value="">, 然后我们就可以通过name, id, class等属性来定位这个输入框。





3.1. 通过id

WebElement element = driver.findElement(By.id("[b]lst-ib"));[/b]

3.2. 通过class name

WebElement element = driver.findElement(By.className("[b][b]lst lst-tbb[/b]"));[/b]

3.3. 通过Name

WebElement element = driver.findElement(By.name("[b][b]q[/b]"));[/b]

3.4. 通过 Tag Name

WebElement frame =driver.findElement(By.tagName("iframe"));

3.5. [b]通过 Link Text[/b]

WebElement cheese=driver.findElement(By.linkText("cheese"));

3.6. [b] 通过 Partial Link Text[/b]

WebElement cheese=driver.findElement(By.partialLinkText("cheese"));

3.7. 通过CSS

WebElementcheese=driver.findElement(By.cssSelector("#food span.dairy.aged"));

3.8. 通过XPATH [注意:XPATH在IE上速度会比较慢,所以推荐使用css selector]

List<WebElement>inputs=driver.findElements(By.xpath("//input"));

3.9. 通过JavaScript

WebElement element=(WebElement)((JavascriptExecutor)driver).executeScript("return
$('.cheese')[0]");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: