Selenium html之于ul标志代码分析与使用
2015-04-09 13:24
260 查看
分析:https://github.com/页面Li
13使用 CSS 选择器 (
可在所检索的项数量上生成断言,如 清单 14所示。
前面的步骤验证了
下一步是检索每个
展示了如何在第一个
您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。
在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了
Assert.assertEquals("Signup and Pricing", anchor1.getText()); A
ssert.assertEquals("Explore GitHub", anchor2.getText());
Assert.assertEquals("Features", anchor3.getText());
Assert.assertEquals("Blog", anchor4.getText());
Assert.assertEquals("Login", anchor5.getText());
<div class="header header-logged-out"> <div class="container clearfix"> <a class="header-logo-wordmark" href="https://github.com/">Github</a> <ul class="top-nav"> <li class="explore"><a href="https://github.com/explore">Explore GitHub</a></li> <li class="search"><a href="https://github.com/search">Search</a></li> <li class="features"><a href="https://github.com/features">Features</a></li> <li class="blog"><a href="https://github.com/blog">Blog</a></li> </ul> <div class="header-actions"> <a class="button primary" href="https://github.com/signup">Sign up for free</a> <a class="button" href="https://github.com/login">Sign in</a> </div> </div>
WebElement element1 = webdriver.findElement(By.id("header")); WebElement element2 = webdriver.findElement(By.name("name")); WebElement element3 = webdriver.findElement(By.tagName("a")); WebElement element4 = webdriver.findElement(By.xpath("//a[@title='logo']")); WebElement element5 = webdriver.findElement(By.cssSelector(".feautures")); WebElement element6 = webdriver.findElement(By.linkText("Blog")); WebElement element7 = webdriver.findElement(By.partialLinkText("Ruby")); WebElement element8 = webdriver.findElement(By.className("login")); |
List<WebElement> webElements = webdriver.findElements(By .xpath("//ul[@class='nav logged_out']/li")); |
By.cssSelector()) 来检索
LI标记。
List<WebElement> webElements = webdriver.findElements(By .cssSelector("ul.nav li")); |
Assert.assertEquals(5, webElements.size()); |
LI标记数量等于 5。
下一步是检索每个
LI标记中的每个锚点(
A标记)。
展示了如何在第一个
LI中获取锚点。此用例使用了 tagName (
By.tagName()) 策略。
WebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); |
WebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); WebElement anchor2 = webElements.get(1).findElement(By.tagName("a")); WebElement anchor3 = webElements.get(2).findElement(By.tagName("a")); WebElement anchor4 = webElements.get(3).findElement(By.tagName("a")); WebElement anchor5 = webElements.get(4).findElement(By.tagName("a")); |
getText()方法。清单 展示了完整的测试方法,以及测试底部的断言。
Assert.assertEquals("Signup and Pricing", anchor1.getText()); A
ssert.assertEquals("Explore GitHub", anchor2.getText());
Assert.assertEquals("Features", anchor3.getText());
Assert.assertEquals("Blog", anchor4.getText());
Assert.assertEquals("Login", anchor5.getText());
相关文章推荐
- 使用htmldom分析HTML代码
- Selenium html之于ul标志代码分析与使用
- C#分析数据库结构,使用XSL模板自动生成代码(二)
- html常用代码分析
- 在struts中html:select 标签的disabled属性中使用java代码
- C#分析数据库结构,使用XSL模板自动生成代码
- C#分析数据库结构,使用XSL模板自动生成代码
- DOS中断跟踪,在纯Dos下使用,分析Dos工作原理的最佳工具,原理详看原代码
- 使用vc连接指定url获得其HTML的代码
- 使用VS2005进行代码覆盖率分析
- C#分析数据库结构,使用XSL模板自动生成代码
- 使用HTML+Javascript制作代码生成工具
- 使用PHP4中的 IntegratedTemplate类实现HTML和PHP代码分离
- C#分析数据库结构,使用XSL模板自动生成代码
- C#分析数据库结构,使用XSL模板自动生成代码
- [翻译]使用HtmlAgilityPack更好的HTML分析和验证
- C#分析数据库结构,使用XSL模板自动生成代码
- [导入]C#分析数据库结构,使用XSL模板自动生成代码
- C#分析数据库结构,使用XSL模板自动生成代码(一)
- HTML多层边框代码结构分析