Page element positioning is the most important thing in automation, and selenium Webdriver provides many methods for element positioning. Testers should be proficient in various positioning methods. The easiest and most stable positioning method is used.
Reading Contents
Automated testing steps
During automated testing, test the usual operation of page elements of the program
1. Find the web page element and assign it to a storage object (WebElement)
2. Operations on objects that store page elements, such as: click on the link, enter characters in the input box, etc.
3. Verify that the elements on the page meet expectations
Through these three steps, we can complete the operation of a page element, and finding the page element is a very important step. The page element cannot be found, and it cannot be done later
The practical complexity of Web page technology makes it difficult to locate a large number of page elements. People often don’t know how to locate it.
Complete location method
Define a Web page element using the findElement function of the WebDriver object
Use the findElements function to locate multiple elements of a page
The targeted page elements need to be stored using WebElement object for subsequent use
The commonly used methods of locating page elements are as follows, sorted by recommendation
Positioning method | Java language implementation example |
id positioning | driver.findElement(By.id("value of id")); |
name positioning | driver.findElement(By.name("name value")); |
All text positioning of links | driver.findElement(By.linkText("all text of the link")); |
Some text positioning of links | driver.findElement(By.partialLinkText("partial text of the link")); |
css positioning | driver.findElement(By.cssSelector("css expression")); |
xpath positioning | driver.findElement(By.xpath("xpath expression")); |
Class Name Positioning | driver.findElement(By.className("class attribute")); |
TagName Tag name targeting | driver.findElement(By.tagName("tagname")); |
Jquery method | Js.executeScript("return jQuery.find("jquery expression")") |
How to locate
When using selenium webdriver for element positioning, the findElement or findElements method is usually used to combine the By class to return element handles to locate elements
The findElement() method returns an element. If not found, an exception will be thrown.
The findElements() method returns multiple elements. If not found, an empty array will be returned and no exception will be thrown.
How to choose a positioning method
The strategy is to choose a simple and stable positioning method.
1. When the page element has an id attribute, try to use id to locate it. If not, choose another positioning method
2. cssSelector executes fast, recommended
3. When positioning hyperlinks, you can consider linkText or partialLinkText: But it should be noted that the text often changes, so it is not recommended to use it.
4. xpath has the most powerful function. The execution speed was slow at that time, because you needed to find the entire DOM, so try to use it as little as possible. When there is really no way, use xpath
Find elements by ID: By.id()
The most recommended way to find elements through the ID of page elements. W3C standard recommends that developers provide unique ID attributes for each page element.
Once an element is given a unique ID attribute, it is easy to locate the element when we do automated testing. The element's ID is used as the preferred identification attribute because it is the fastest identification strategy.
Taking Baidu homepage as an example, the HTML sample code of the search box is as follows, its ID is kw
<input type="text" autocomplete="off" maxlength="100" id="kw" name="wd">
The HTML sample code for the "Baidu" search button element is as follows, its ID is su
<input type="submit" id="su" value="Baidu">
Java sample code for finding elements by ID in Selenium/WebDriver is as follows
WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); WebElement searchBox = driver.findElement(By.id("kw")); searchBox.sendKeys("Little Tank Blog Park"); WebElement searchButton = driver.findElement(By.id("su")); searchButton.submit(); driver.close();Find elements by Name: By.name()
Taking Douban.com's homepage search box as an example, the HTML code of its search box is as follows, its name is: q
<input type="text" autocomplete="off" name="q" placeholder="books, movies, music, groups, stations, members" size="12" maxlength="60">
The Java code in WebDriver to search for search boxes on Douban homepage through name is as follows:
WebDriver driver = new FirefoxDriver(); driver.get("http://www.douban.com"); WebElement searchBox = driver.findElement(By.name("q")); searchBox.sendKeys("Small Tank"); searchBox.submit();Find elements by TagName: By.tagName()
When searching for elements through tagName, multiple elements will be returned. Therefore, findElements() is required.
WebDriver driver = new FirefoxDriver(); driver.get("http://www.cnblogs.com"); List<WebElement> buttons = driver.findElements(By.tagName("div")); System.out.println("Button:" + buttons.size());Note: If you use tagName, you should note that the tagName of many HTML elements is the same.
For example, radio box, check box, text box, password box. These element tags are all input. At this time, the tagName alone cannot accurately obtain the elements we want. You also need to combine the type attribute to filter out the elements we want.
WebDriver driver = new FirefoxDriver(); driver.get("http://www.cnblogs.com"); List<WebElement> buttons = driver.findElements(By.tagName("input")); for (WebElement webElement : buttons) { if (webElement.getAttribute("type").equals("text")) { System.out.println("input text is :" + webElement.getText()); } }Find elements by ClassName
Taking Taobao’s homepage search as an example, the HTML code of its search box is as follows:
<input autocomplete="off" autofocus="true" accesskey="s" aria-label="Please enter search text" name="q" id="q" aria-haspopup="true" aria-combobox="list" role="combobox" x-webkit-grammar="builtin:translate" tabindex="0">
The Java sample code is as follows
WebDriver driver = new FirefoxDriver(); driver.get("http://www.taobao.com"); Thread.sleep(15000); WebElement searchBox = driver.findElement(By.className("search-combobox-input")); searchBox.sendKeys("down jacket"); searchBox.submit(); Note: When using className to locate elements, you sometimes encounter a
Find elements by LinkText By.linkText();
Position elements directly through text information on the hyperlink: e.g.
<a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" onclick="return false;">Login</a>
The HTML code is as follows
WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); WebElement loginLink = driver.findElement(By.linkText("Login")); loginLink.click();Find elements by PartialLinkText()
This method is an enhanced version of the previous method. When you only want to match with some keywords, you can use this method to locate elements through some hyperlink text.
The HTML code is as follows
WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); WebElement loginLink = driver.findElement(By.partialLinkText("log")); loginLink.click();Note: When using this method to locate, the problem may be caused is that when you do not know that a hyperlink contains "etc". The findElement method will only return the first element found, and will not return all elements that meet the criteria.
If you want to get all the elements that meet the criteria, you can only use the findElements method.
The above is the information on java selenium element positioning. We will continue to add relevant information in the future. Thank you for your support!