※ The importance of positioning elements: And find elements that perform element
Three methods of positioning elements
- 1, positioning a single element: when targeting a single element, selenium-webdriver suggests the following methods of positioning elements. In the positioning mode, the priority use id, name, classname, for online link element is recommended linkText targeting, positioning is not good for the element, consider using Firefox plug-ins to assist positioning (xpath).
- 2, a plurality of positioning elements
- 3, Level positioning: level positioning idea is to locate the parent element, then the parent element to pinpoint their chosen child elements we need. The general level of positioning application scenarios are unable to locate directly to the need to select the elements, but the parent element relatively easy to locate, and then traverse the target element of its child elements to select the desired positioning by the parent element, or the need to locate all of the child elements of an element . The typical application is the positioning of the table.
findElement method and findElements
-
findElement () Returns a WebElement element
-
findElements () Returns a List, a plurality of elements WebElement
Eight kinds of targeting
• By.id (id): Find by ID attribute
• By.name (name): Find a property by name
• By.className (className): Find by classname property
• By.linkText (link text): The link text
• By.partialLinkText (section link text): by section link text
• By.cssSelector (Css path): Path through CSS
• By.tagName (name): Find by tagname
• By.xpath (XPath path): Find by XPath
For example: the above mentioned id \ name \ className
代码如下:
import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ZmbxTestJC { @Test public void Testid() throws InterruptedException { WebDriver driver =new ChromeDriver(); String url = "https://www.baidu.com"; driver.get(url); driver.findElement (By.name ( "WD")) sendKeys ( "the Selenium");. // target name, the input content driver.findElement (By.id ( "SU")) the Click ();. // Location the id, click on the Baidu search query driver.quit (); } }
For example: By.linkText (Link text)
HTML Source: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html"> search settings </a> WebElementelement = driver.findElement (By.linkText ( "Search Settings" ));
For example: By.partialLinkText (section link text)
HTML Source: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html"> search settings </a> WebElementelement = driver.findElement (By partialLinkText ( "Search"). ); // search settings
For example: By.cssSelector (Css path)
HTML 源码: <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a> WebElementelement=driver.findElement(By. cssSelector( "#u1 > a:nth-child(1)" ));
例如:By.tagName(name)
HTML 源码: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索设置</a> WebElementelement=driver.findElement(By. tagName( "a" ));
For example: By.xpath (the XPath path)
HTML 源码: <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a> WebElementelement=driver.findElement(By. xpath( "//*[@id="u1"]/a[1]" ));
注意:
1.使用findElement()方法查找元素,元素必须是唯一
2.findElements()同样支持这八种定位方式,只是获取的是多个元素,返回List
XPATH description:
XPath is a finding information in an XML document language. XPath is used to navigate through elements and attributes in an XML document.
In XPath, there are seven types of nodes: element, attribute, text, namespaces, processing instructions, comments and document node (or root). XML document is treated as a node tree. The root of the tree is called the document node or root node.
Look at the following XML document:
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book> </bookstore>
上面的XML文档中的节点例子:
<bookstore> (文档节点)
<author>J K. Rowling</author> (元素节点)
lang="en" (属性节点)
Select node
XPath uses path expressions to select nodes in an XML document. Along the path through the node, or to select a step.
Listed below are the most useful path expressions:
expression | description |
---|---|
nodename | Select all the child nodes of this node. |
/ | Choose from the root node. |
// | Select the document matches the selected node from the current node, regardless of their location. |
. | Select the current node. |
.. | Select the parent of the current node. |
@ | Select Properties. |
Example, in the table below, we've listed some of the results of path expressions and expressions:
Path expression | result |
---|---|
bookstore | Selects all child nodes of the bookstore element. |
/bookstore | Select the root element bookstore. NOTE: If the path starts with a forward slash (/), This path is always representative of the absolute path to an element! |
bookstore/book | All of the selected sub-elements belonging to the bookstore book elements. |
//book | Select all book sub-elements, regardless of their Position in the document. |
bookstore//book | The choice belongs to the bookstore element offspring There are book elements, regardless of their bookstore is located Under what position. |
// @ lang | Select all of the property named lang. |
XPath axes
With respect to the axis defined set of nodes of the current node.
Axis name | result |
---|---|
ancestor | Select all ancestors (parent, grandparent, etc.) of the current node. |
ancestor-or-self | Select all ancestors (parent, grandparent, etc.) of the current node and the current node itself. |
attribute | Select all the attributes of the current node. |
child | Select all the child elements of the current node. |
descendant | Select all descendants of the current node elements (son, Sun, etc.). |
descendant-or-self | 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 |
following | 选取文档中当前节点的结束标签之后的所有节点。 |
namespace | 选取当前节点的所有命名空间节点。 |
parent | 选取当前节点的父节点。 |
preceding | 选取文档中当前节点的开始标签之前的所有节点。 |
preceding-sibling | 选取当前节点之前的所有同级节点。 |
self | 选取当前节点。 |
详细了解xpath,请参照 https://www.w3school.com.cn/xpath/xpath_summary.asp