Selenium HTML to the UL Mark Code analysis and use

Source: Internet
Author: User
Tags gettext tagname xpath

Analysis: https://github.com/page li

<Divclass= "header Header-logged-out" ><Divclass= "Container Clearfix" ><AClass= "Header-logo-wordmark"href= "https://github.com/" >github</A><Ulclass= "Top-nav" ><Liclass= "Explore" ><Ahref= "Https://github.com/explore" >explore github</a></Li><Liclass= "Search" ><Ahref= "Https://github.com/search" >search</a></Li><LiClass= "Features" ><Ahref= "Https://github.com/features" >features</a></Li><Liclass= "Blog" ><Ahref= "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 Use the CSS selector ( By.cssSelector() ) to retrieve the LI tag.

List<webelement> webelements = webdriver.findelements (by             . Cssselector ("Ul.nav li"));

An assertion can be generated on the number of items retrieved, as shown in Listing 14.

Assert.assertequals (5, Webelements.size ());
The previous steps verify that the LIThe number of tokens equals 5.

The next step is LI to retrieve each anchor point (marker) in each tag A .

Shows how to get an anchor point in the first LI . This use case uses the TagName ( By.tagName() ) policy.

Webelement Anchor1 = webelements.get (0). Findelement (By.tagname ("a"));

You can use a similar method to collect all 5 anchor points, as shown in Listing 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"));

At this stage, you can verify that the text within the anchor point is consistent with the expected string. To retrieve the text within a tag, Webdriver provides a getText() method. The checklist shows the complete test method and the assertion at the bottom of the test.

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

Selenium HTML to the UL Mark Code analysis and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.