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]");
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]");
相关文章推荐
- Hprose question
- C#中Request.servervariables参数
- torque 安装排错
- Unigui unidbgrid+unidac uniquery联合使用时产生的奇葩问题
- [SoapUI] 通过Groovy Script获取当前运行的是哪套Environment
- leetcode之Range Sum Query 2D - Immutable
- Machine Learning week 9 quiz: Recommender Systems
- 检测到有潜在危险的 Request.Form 值
- 255. Verify Preorder Sequence in Binary Search Tree
- Parquet_2. 在 Impala/Hive 中使用 Parquet 格式存储数据
- Parquet_1. 使用谓词下推和映射来优化 Job
- UOJ UER#5 A 万圣节的南瓜灯
- UOJ UER#5 A 万圣节的南瓜灯
- java swing MenuItem乱码
- [Android--UI]用Fragments创建动态UI
- UI设计师工作必备的七个设计神器
- Android-AlertDialog为什么用builder类创建对象
- druid 1.0.16数据库密码加密
- RequireJS
- OpenCV - Mac上使用HighGUI