"Selenium webdriver" element positioning function findelement

Source: Internet
Author: User

Locate the elements on the Web page, using the Findelement function, which can be quickly positioned based on the different attributes of the element. The specific properties are as follows:

Example:

HTML paging File:

1<Htmlxmlns= "Http://www.w3.org/1999/xhtml"Lang= "en-US">2<Head>3<Body>4<FormName= "LoginForm">5<LabelFor= "username">username:</Label><InputType= "Text"Name= "username"Id= "UID"/></Br>6<LabelFor= "Password">password:</Label><InputType= "Password"Name= "Password"Id= "pid"/></br> 7 <input name= " Login " Type=" Submit " Value= "Login" /> 8 Span style= "color: #0000ff;" ></form> 9 </body>10 Span style= "color: #0000ff;" ></head>        
1  Packagecom.annieyu.test;2 Importorg.openqa.selenium.By;3 ImportOrg.openqa.selenium.WebDriver;4 Importorg.openqa.selenium.WebElement;5 ImportOrg.openqa.selenium.firefox.FirefoxDriver;6 7  Public classfindelement {8      Public Static voidMain (string[] args) {9Webdriver Driver =Newfirefoxdriver ();Ten         //HTML page file path OneString urlpath= ("file:///D:/AnnieJava/HTML/ExamplePage.html"); A          -         //opens the specified URL - driver.navigate (). to (URLPath); the          -         //Findelementbyid find elements on a page -Webelement UserID = driver.findelement (by.id ("UID")); -         //Findelementbyname find elements on a page +Webelement userName = driver.findelement (By.name ("UserName")); -          + System.out.println (Username.gettagname ()); A     } at}

The id attribute is most commonly used, and if the element on the page has a unique ID identifier, the element can be quickly positioned using the id attribute.

However, sometimes the element may not have an id attribute, or the ID is not unique, or the id attribute is dynamically generated, consider using the Name property.

In addition to using the Id,name attribute, we can also use the class attribute to locate the element, the class attribute is used in order to use CSS.

1 <HTMLxmlns= "http://www.w3.org/1999/xhtml"Lang= "en -us">2 <Head>3 <Body>4 <formname= "LoginForm">5 <label for= "username">UserName:</label> <inputclass= "username"> </BR>6 <label for= "Password">Password:</label> <inputclass= "Password" /> </BR>7 <inputname= "Login"type= "Submit"value= "Login"/>8 </form>9 </Body>Ten </Head>
1  Packagecom.annieyu.test;2 Importorg.openqa.selenium.By;3 ImportOrg.openqa.selenium.WebDriver;4 Importorg.openqa.selenium.WebElement;5 ImportOrg.openqa.selenium.firefox.FirefoxDriver;6 7  Public classfindelement {8      Public Static voidMain (string[] args) {9Webdriver Driver =Newfirefoxdriver ();Ten         //HTML page file path OneString urlpath= ("file:///D:/AnnieJava/HTML/ExamplePage.html"); A          -         //opens the specified URL - driver.navigate (). to (URLPath); the          -         //findelement Byclassname find elements on a page -Webelement className = driver.findelement (By.classname ("username")); -     } +}


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.