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