name positioning
Positioning by name is another common way of positioning elements.
When an element has a name attribute, you can use name to locate it, and still take the Bing search box as an example (name="q"). Example: find_element_by_name("q"), as shown in the figure.
The attribute description HTML code of the search box element is as follows:
`<input class="b_searchbox" id="sb_form_q" name="q" title="输入搜索词" type="search" value="" maxlength="100" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" aria-controls="sw_as" aria-autocomplete="both" aria-owns="sw_as">``
Selenium uses the FireFox browser to drive and operate the input box code as follows:
from selenium import webdriver
driver=webdriver.Firefox()
driver.get("https://cn.bing.com/")
driver. find_element_by_name("q").send_keys("bella")
driver.quit() # 关闭浏览器
class positioning
Most of the front-end styles are rendered by class, so when positioning elements, you can also locate them by selecting class. Class is used to associate attributes defined in CSS.
Bing homepage search box class=" b_searchbox". As shown in Figure 5-8.
Use: find_element_by_class_name("b_searchbox") to locate the search box.
The attribute description HTML code of the search box element is as follows:
<input class="b_searchbox" id="sb_form_q" name="q" title="输入搜索词" type="search" value="" maxlength="100" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" aria-controls="sw_as" aria-autocomplete="both" aria-owns="sw_as">
The attributes of the search box element, as shown in the figure
Selenium uses the FireFox browser to drive and operate the input box code as follows:
from selenium import webdriver
driver= webdriver.Firefox()
driver.get("https://cn.bing.com/")
driver.find_element_by_class_name("b_searchbox").send_keys("bella")
driver.quit() # 关闭浏览器
Note:
Sometimes the class attribute value of an element is composed of two values separated by a space (such as [Baidu] button element class="bg s_btn"). At this time, when positioning by class, only one of them can be used (such Just use "s_btn" in class="bg s_btn"), as shown in the figure.
Take Baidu search page as an example to explain.
- First, find the search box and [Baidu click] button;
- Enter the searched keywords through the keyboard;
- Use the mouse to click the [Baidu once] button;
- Submit a search request.
The attribute description HTML code of Baidu (Baidu) homepage search box element is as follows:
<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
Radio button Elements, as shown.
Baidu (Baidu) homepage [on Baidu] The attribute description HTML code of the button element is as follows:
`<input type="submit" id="su" value="百度一下" class="bg s_btn">`
Code:
from selenium import webdriver
driver= webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_id("kw").send_keys("bella") #通过id定位搜索框元素且赋值bella
driver.find_element_by_class_name("bg s_btn").click() #通过class定位【百度一下】按钮并单击
Run the above code, you can see the following error in the console in PyCharm.Selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .bg s_btn
Change the "bg s_btn" in the driver.find_element_by_class_name("bg s_btn") code to "s_btn":
from selenium import webdriver
driver= webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_id("kw").send_keys("bella")
#driver.find_element_by_class_name("bg s_btn").click()
driver.find_element_by_class_name("s_btn").click()
Run the code and observe that you can successfully click the [Baidu Click] button without reporting an error. When the code is running, the verification information shown in Figure 5-11 appears after clicking the [Baidu Click] button, which is a method of Baidu's security verification, and there is no need to care about it.
Change the "bg s_btn" in the driver.find_element_by_class_name("bg s_btn") code to "bg":
from selenium import webdriver
driver= webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_id("kw").send_keys("bella")
#driver.find_element_by_class_name("bg s_btn").click()
driver.find_element_by_class_name("bg").click()
Run the code and observe that you can successfully enter bella in the search box, but the [Baidu click] button is indeed not clicked. The code has finished running and no errors are reported.
Taking the Baidu search page as an example, if you locate the [Baidu One] button element through the find_element_by_class_name method, the following conclusions can be drawn.
- It is not possible to use the class attribute value "bg s_btn" directly, the code is wrong;
- Change "bg s_btn" to "bg". Although the code ends normally without an error, the button element of [Baidu] will not be clicked;
- "bg s_btn" is changed to "s_btn", the code can run normally, and the button element of [Baidu Click] can be clicked correctly.
Note:
ID, name, and classes are the most commonly used methods to locate elements.
[Test the full series of video courses] Please click me...
( https://edu.51cto.com/lecturer/968349.html )
Selenium3 automated testing [13] FireFox and Chrome browser element positioning
Books are available on JD.com and Dangdang on
JD.com: https://item.jd.com/12784287.html
Dangdang: http://product.dangdang.com/29177828.html
Selenium3 automated testing [13] FireFox and Chrome browser element positioning