您的位置:首页 > Web前端 > HTML

Selenium html之于ul标志代码分析与使用

2015-04-09 13:24 260 查看
分析:https://github.com/页面Li

<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"));

13使用 CSS 选择器 (
By.cssSelector()
) 来检索
LI
标记。

List<WebElement> webElements = webdriver.findElements(By
.cssSelector("ul.nav li"));

可在所检索的项数量上生成断言,如 清单 14所示。

Assert.assertEquals(5, webElements.size());

前面的步骤验证了
LI
标记数量等于 5。
下一步是检索每个
LI
标记中的每个锚点(
A
标记)。

展示了如何在第一个
LI
中获取锚点。此用例使用了 tagName (
By.tagName()
) 策略。

WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));

您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。

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"));

在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了
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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: