Selenium 简明教程
Selenium Webdriver - Identify Multiple Elements
在本章中,我们将学习如何通过各种选项来识别多个元素。让我们从理解通过 ID 识别多个元素开始。
In this chapter, we will learn how to identify multiple elements by various options. Let us begin by understanding identifying multiple elements by Id.
By id
不建议通过定位器 ID 识别多个元素,因为 id 属性的值对于一个元素是唯一的,并且适用于页面上的单个元素。
It is not recommended to identify multiple elements by the locator id, since the value of an id attribute is unique to an element and is applicable to a single element on the page.
By Class name
一旦我们导航到一个网页,我们就必须与页面上可用的网络元素交互,例如单击链接/按钮、在编辑框中输入文本等等,才能完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的首要任务是识别元素。我们可以对元素使用 class 属性进行识别,并利用方法 find_elements_by_class_name。这样,所有具有与属性 class 匹配值的所有元素都以列表形式返回。
For this, our first job is to identify the elements. We can use the class attribute for elements for their identification and utilise the method find_elements_by_class_name. With this, all the elements with the matching value of the attribute class are returned in the form of list.
如果不存在具有类属性匹配值的元素,则返回一个空列表。
In case there are no elements with the matching value of the class attribute, an empty list shall be returned.
识别多个 Classname 的 syntax 如下 −
The syntax for identifying multiple elements by Classname is as follows −
driver.find_elements_by_class_name("value of class attribute")
让我们看看具有如下所示 class 属性的网络元素的 html 代码 −
Let us see the html code of webelements having class attribute as given below −
上图中突出显示的 class 属性的值为 toc chapters。让我们尝试计算此类网络元素的数量。
The value of the class attribute highlighted in the above image is toc chapters. Let us try to count the number of such webelements.
Code Implementation
通过 Classname 识别多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Classname is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify elements with class attribute
l = driver.find_elements_by_class_name("chapters")
#count elements
s = len(l)
print('Count is:')
print(s)
#driver quit
driver.quit()
输出显示的消息为 - 进程退出代码 0,这意味着上面的 Python 代码执行成功。此外,具有类属性值 chapters(从 len 方法获得)的网络元素总数(2)将打印在控制台中。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the total count of webelements having the class attributes value chapters (obtained from the len method) - 2 gets printed in the console.
By Tagname
一旦我们导航到一个网页,我们就必须与页面上可用的网络元素交互,例如单击链接/按钮、在编辑框中输入文本等等,才能完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的首要任务是识别元素。我们可以对元素使用标签名进行识别,并利用方法 find_elements_by_tag_name。这样,所有具有与标签名匹配值的所有元素都以列表形式返回。
For this, our first job is to identify the elements. We can use the tagname for elements for their identification and utilise the method find_elements_by_tag_name. With this, all the elements with the matching value of the tagname are returned in the form of list.
如果不存在具有与标签名匹配值的元素,则返回一个空列表。
In case there are no elements with the matching value of the tagname, an empty list shall be returned.
通过 Tagname 标识多个元素的 syntax 如下所示 −
The syntax for identifying multiple elements by Tagname is as follows −
driver.find_elements_by_tag_name("value of tagname")
让我们看看 webelement 的 html 代码,如下所示 −
Let us see the html code of a webelement, which is as follows −
上图中突出显示的 tagname 的值为 h4。让我们尝试计算具有 tagname 为 h4 的 webelement 的数量。
The value of the tagname highlighted in the above image is h4. Let us try to count the number of webelements having tagname as h4.
Code Implementation
通过 Tagname 标识多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Tagname is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify elements with tagname
l = driver.find_elements_by_tag_name("h4")
#count elements
s = len(l)
print('Count is:')
print(s)
#driver quit
driver.quit()
输出显示消息 - 退出代码为 0 的进程,表示上面的 Python 代码执行成功。此外,具有 tagname 为 h4 的 webelement 的总数(从 len 方法中获取) - 1 将打印在控制台中。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the total count of webelement having the tagname as h4 (obtained from the len method) - 1 gets printed in the console.
By Partial Link Text
一旦我们导航到一个网页,我们可能需要通过单击链接与 webelement 进行交互以完成我们的自动化测试用例。部分链接文本用于具有锚标记的元素。
Once we navigate to a webpage, we may have to interact with the webelements by clicking a link to complete our automation test case. The partial link text is used for elements having the anchor tag.
为了达到这个目的,我们的首要任务是标识元素。我们可以为元素使用部分链接文本属性以对其进行标识并使用 find_elements_by_partial_link_text 方法。这样,所有具有给定部分链接文本匹配值的元素将以列表形式返回。
For this, our first job is to identify the elements. We can use the partial link text attribute for elements for their identification and utlize the method find_elements_by_partial_link_text. With this, all the elements with the matching value of the given partial link text are returned in the form of a list.
如果没有具有该部分链接文本匹配值的元素,则返回一个空列表。
In case there are no elements with the matching value of the partial link text, an empty list shall be returned.
通过部分链接文本标识多个元素的 syntax 如下所示 −
The syntax for identifying multiple elements by Partial Link Text is as follows −
driver.find_elements_by_partial_link_text("value of partial link text")
让我们看看链接的 html 代码,如下所示 −
Let us see the html code of link, which is as follows −
上图中突出显示的链接 - 使用条款具有 tagname - a 和部分链接文本 - 条款。让我们在对其进行标识后尝试获取该文本。
The link highlighted - Terms of Use in the above image has a tagname - a and the partial link text - Terms. Let us try to identify the text after identifying it.
Code Implementation
通过部分链接文本标识多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Partial Link Text is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify elements with partial link text
l = driver.find_elements_by_partial_link_text('Terms')
#count elements
s = len(l)
#iterate through list
for i in l:
#obtain text
t = i.text
print('Text is: ' + t)
#driver quit
driver.quit()
输出显示消息 - 退出代码为 0 的进程,表示上面的 Python 代码执行成功。此外,具有部分链接文本定位器(从文本方法中获取)标识的链接文本 - 使用条款将打印在控制台中。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the text of the link identified with the partial link text locator (obtained from the text method) - Terms of use gets printed in the console.
By Link Text
一旦我们导航到一个网页,我们可能需要通过单击链接与 webelement 进行交互以完成我们的自动化测试用例。链接文本用于具有锚标记的元素。
Once we navigate to a webpage, we may have to interact with the webelements by clicking a link to complete our automation test case. The link text is used for elements having the anchor tag.
为了达到这个目的,我们的首要任务是标识元素。我们可以为元素使用链接文本属性以对其进行标识并使用 find_elements_by_link_text 方法。这样,所有具有给定链接文本匹配值的元素将以列表形式返回。
For this, our first job is to identify the elements. We can use the link text attribute for elements for their identification and utilize the method find_elements_by_link_text. With this, all the elements with the matching value of the given link text are returned in the form of a list.
如果没有具有该链接文本匹配值的元素,则返回一个空列表。
In case there are no elements with the matching value of the link text, an empty list shall be returned.
通过链接文本标识多个元素的 syntax 如下所示 −
The syntax for identifying multiple elements by Link Text is as follows −
driver.find_elements_by_link_text("value of link text")
让我们看看链接的 html 代码,如下所示 −
Let us see the html code of link, which is as follows −
上图中突出显示的链接 - Cookie 政策具有 tagname - a 和链接文本 - Cookie 政策。让我们在对其进行标识后尝试获取该文本。
The link highlighted - Cookies Policy in the above image has a tagname - a and the link text - Cookies Policy. Let us try to identify the text after identifying it.
Code Implementation
通过链接文本标识多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Link Text is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify elements with link text
l = driver.find_elements_by_link_text('Cookies Policy')
#count elements
s = len(l)
#iterate through list
for i in l:
#obtain text
t = i.text
print('Text is: ' + t)
#driver quit
driver.quit()
输出显示了信息 - 进程退出代码 0,表明上述 Python 代码已成功执行。此外,带有链接文本定位器(从 text 方法获取)的链接文本 - Cookies 政策将在控制台打印出来。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the text of the link identified with the link text locator (obtained from the text method) - Cookies Policy gets printed in the console.
By Name
一旦我们导航到一个网页,我们就必须与页面上可用的网络元素交互,例如单击链接/按钮、在编辑框中输入文本等等,才能完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的第一个任务是识别元素。我们可以使用元素的 name 属性进行识别并使用 find_elements_by_name 方法。使用此方法,具有匹配属性值 name 的元素将以列表的形式返回。
For this, our first job is to identify the elements. We can use the name attribute of elements for their identification and utilize the method find_elements_by_name. With this, the elements with the matching value of the attribute name are returned in the form of a list.
如果没有与 name 属性匹配值的元素,则将返回一个空列表。
In case there is no element with the matching value of the name attribute, an empty list shall be returned.
用于按名称识别多个元素的 syntax 如下 −
The syntax for identifying multiple elements by Name is as follows −
driver.find_elements_by_name("value of name attribute")
让我们看看以下 web 元素的 html 代码 −
Let us see the html code of an webelement, which is as follows −
上图中突出显示的编辑框具有值为 search 的 name 属性。让我们尝试识别该编辑框后向其中输入一些文本。
The edit box highlighted in the above image has a name attribute with value search. Let us try to input some text into this edit box after identifying it.
Code Implementation
通过名称识别多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Name is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify elements with name attribute
l = driver.find_elements_by_name('search')
#count elements
s = len(l)
#iterate through list
for i in l:
#obtain text
t = i.send_keys('Selenium Python')
v = i.get_attribute('value')
print('Value entered is: ' + v)
#driver quit
driver.quit()
输出显示了信息 - 进程退出代码 0,表明上述 Python 代码已成功执行。此外,在编辑框中输入的值(从 get_attribute 方法获取) - Selenium Python 会在控制台打印出来。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the value entered within the edit box (obtained from the get_attribute method) - Selenium Python gets printed in the console.
By CSS Selector
一旦我们导航到一个网页,我们就必须与页面上可用的网络元素交互,例如单击链接/按钮、在编辑框中输入文本等等,才能完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的第一个任务是识别元素。我们可以为它们创建 css 选择器并使用 find_elements_by_css_selector 方法。使用此方法,具有匹配给定 css 值的元素将以列表的形式返回。
For this, our first job is to identify the elements. We can create a css selector for their identification and utilize the method find_elements_by_css_selector. With this, the elements with the matching value of the given css are returned in the form of list.
如果没有与 css 匹配值的元素,则将返回一个空列表。
In case there is no element with the matching value of the css, an empty list shall be returned.
用于通过 CSS 选择器识别多个元素的 syntax 如下 −
The syntax for identifying multiple elements by CSS Selector is as follows −
driver.find_elements_by_css_selector("value of css")
Rules for CSS Expression
创建 css 表达式的规则在下面讨论 −
The rules to create a css expression are discussed below −
-
To identify the element with css, the expression should be tagname[attribute='value']. We can also specifically use the id attribute to create a css expression.
-
With id, the format of a css expression should be tagname#id. For example, input#txt [here input is the tagname and the txt is the value of the id attribute].
-
With class, the format of css expression should be tagname.class . For example, input.cls-txt [here input is the tagname and the cls-txt is the value of the class attribute].
-
If there are n children of a parent element, and we want to identify the nth child, the css expression should have nth-of –type(n).
在上面的代码中,如果我们想识别 ul[Questions and Answers] 的第四个 li 子元素,则 css 表达式应为 ul.reading li:nth-of-type(4)。类似地,要识别最后一个子元素,css 表达式应为 ul.reading li:last-child。
In the above code, if we want to identify the fourth li child of ul[Questions and Answers], the css expression should be ul.reading li:nth-of-type(4). Similarly, to identify the last child, the css expression should be ul.reading li:last-child.
对于值动态更改的属性,我们可以使用 ^= 来查找其属性值以特定文本开头的元素。例如,input[name^='qa'] [这里输入是标签名,name 属性的值以 qa 开头]。
For attributes whose values are dynamically changing, we can use ^= to locate an element whose attribute value starts with a particular text. For example, input[name^='qa'] [here input is the tagname and the value of the name attribute starts with qa].
对于值动态更改的属性,我们可以使用 $= 来查找其属性值以特定文本结尾的元素。例如,input[class$='txt'] 这里,输入是标签名,class 属性的值以 txt 结尾。
For attributes whose values are dynamically changing, we can use $= to locate an element whose attribute value ends with a particular text. For example, input[class$='txt'] Here, input is the tagname and the value of the class attribute ends with txt.
对于值动态变化的属性,我们可以使用 = to locate an element whose attribute value contains a specific sub-text. For example, input[name ='nam']。此处,input 是标签名称,而 name 属性的值包含子文本 nam。
For attributes whose values are dynamically changing, we can use = to locate an element whose attribute value contains a specific sub-text. For example, input[name='nam'] Here, input is the tagname and the value of the name attribute contains the sub-text nam.
让我们看看一个 web 元素的 html 代码:
Let us see the html code of a webelement −
上面图像中突出显示的编辑框具有值的搜索 name 属性,css 表达式应为 input[name='search']。让我们在 identification 它后尝试在此编辑框中输入一些文本。
The edit box highlighted in the above image has a name attribute with value search, the css expression should be input[name='search']. Let us try to input some text into this edit box after identifying it.
Code Implementation
通过 CSS 选择器识别多个元素的代码实现如下:
The code implementation for identifying multiple elements by CSS Selector is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify elements with css
l = driver.find_elements_by_css_selector("input[name='search']")
#count elements
s = len(l)
#iterate through list
for i in l:
#obtain text
t = i.send_keys('Tutorialspoint')
v = i.get_attribute('value')
print('Value entered is: ' + v)
#driver quit
driver.quit()
输出显示消息 - 进程退出代码为 0,表示上面的 Python 代码已成功执行。此外,在编辑框内输入的值(从 get_attribute 方法获取) - Tutorialspoint 会打印在控制台中。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the value entered within the edit box (obtained from the get_attribute method) - Tutorialspoint gets printed in the console.
By Xpath
一旦我们导航到一个网页,我们就必须与页面上可用的网络元素交互,例如单击链接/按钮、在编辑框中输入文本等等,才能完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的第一个任务是识别元素。我们可以为它们的 identification 创建一个 xpath,并使用 find_elements_by_xpath 方法。这样,具有给定 xpath 匹配值 的元素将以列表的形式返回。
For this, our first job is to identify the elements. We can create an xpath for their identification and utilize the method find_elements_by_xpath. With this, the elements with the matching value of the given xpath are returned in the form of a list.
如果没有元素具有与 xpath 匹配的值,则将返回一个空列表。
In case there is no element with the matching value of the xpath, an empty list shall be returned.
通过 Xpath identification 多个元素的 syntax 如下:
The syntax for identifying multiple elements by Xpath is as follows:
driver.find_elements_by_xpath("value of xpath")
Rules for Xpath Expression
创建 xpath 表达式的规则如下所述:
The rules to create a xpath expression are discussed below −
-
To identify the element with xpath, the expression should be //tagname[@attribute='value']. There can be two types of xpath – relative and absolute. The absolute xpath begins with / symbol and starts from the root node upto the element that we want to identify.
例如,
For example,
/html/body/div[1]/div/div[1]/a
-
The relative xpath begins with // symbol and does not start from the root node.
例如,
For example,
//img[@alt='tutorialspoint']
让我们从根部开始看高亮链接 - 主页的 html 代码。
Let us see the html code of the highlighted link - Home starting from the root.
元素主页的绝对 xpath 如下:
The absolute xpath for the element Home can be as follows −
/html/body/div[1]/div/div[1]/a.
元素主页的相对 xpath 可以如下:
The relative xpath for element Home can be as follows −
//a[@title='TutorialsPoint - Home'].
Functions
还有一些函数可以帮助构建相对 xpath 表达式:
There are also functions available which help to frame relative xpath expressions −
text()
text()
它用于借助页面上的可见文本来 identification 元素。xpath 表达式如下:
It is used to identify an element with the help of the visible text on the page. The xpath expression is as follows −
//*[text()='Home'].
starts-with
starts-with
它用于 identification 其属性值以特定文本开头的元素。此函数通常用于在每个页面加载时其值都会发生变化的属性。
It is used to identify an element whose attribute value begins with a specific text. This function is normally used for attributes whose value changes on each page load.
让我们查看元素 Q/A 的 html −
Let us see the html of the element Q/A −
xpath 表达式应如下 −
The xpath expression should be as follows −
//a[starts-with(@title, 'Questions &')].
contains()
contains()
它标识一个其属性值包含子文本的元素。此函数通常用于在每次页面加载时都会更改其值的属性。
It identifies an element whose attribute value contains a sub-text. This function is normally used for attributes whose value changes on each page load.
xpath 表达式如下 −
The xpath expression is as follows −
//a[contains(@title, 'Questions & Answers')].
让我们看看一个 web 元素的 html 代码:
Let us see the html code of a webelement −
上图中高亮的编辑框有一个名为搜索的属性,xpath 表达式应为 //input[@name='search']。让我们在识别出此编辑框后尝试在其中输入一些文本。
The edit box highlighted in the above image has a name attribute with value search, the xpath expression should be //input[@name='search']. Let us try to input some text into this edit box after identifying it.
Code Implementation
通过 Xpath 识别多个元素的代码实现如下 −
The code implementation for identifying multiple elements by Xpath is as follows −
from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify elements with xpath
l = driver.find_elements_by_xpath("//input[@name='search']")
#count elements
s = len(l)
#iterate through list
for i in l:
#obtain text
t = i.send_keys('Tutorialspoint - Selenium')
v = i.get_attribute('value')
print('Value entered is: ' + v)
#driver quit
driver.quit()
输出显示消息 - 进程退出代码 0,表示上述 Python 代码已成功执行。此外,在编辑框中输入的值(从 get_attribute 方法获取) - Tutorialspoint - Selenium 将打印在控制台中。
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the value entered within the edit box (obtained from the get_attribute method) - Tutorialspoint - Selenium gets printed in the console.