Article Directory
1. Browser web page debugging
In the Chrome browser, click the right mouse button and select the "Check" option in the pop-up shortcut menu; in the pop-up debugging information window, after clicking the button, move the mouse to the target location that needs to be located, and the debugging information window will appear Display the attributes of the element. Take the test of Baidu search page as an example. After clicking the [illustration] button in the debugging information window, move the mouse to the Baidu search input box, and the element attributes will be displayed on the screen. As shown in the figure
, the element attribute ID of the Baidu search input box is kw, NAME is wd, and CLASS_NAME is s_ipt.
Or go to Options>More Tools>Developer Tools
and click the specified location.
2. Element positioning
In Selenium automated testing, a single element positioning method and multiple element positioning methods are provided. Both methods are based on element attribute ID, NAME, CLASS_NAME, TAG_NAME, CSS_SELECTOR, XPATH, LINK_TEXT, PARTIAL_LINK_TEXT to locate. The following is a specific example to illustrate the application of a single element positioning in the UI automation test.
- 1.find_element_by_id
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("http://www.baidu.com")
driver.find_element_by_id('kw').send_keys('Selenium')
- 2.find_element_by_name
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("http://www.baidu.com")
driver.find_element_by_name('wd').send_keys('Selenium')
- 3.find_element_by_class_name
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("http://www.baidu.com")
driver.find_element_by_class_name('s_ipt').send_keys('Selenium')
- 4. find_element_by_xpath
locates the element of Baidu search input box through XPATH, the method is find_element_by_xpath, the original attribute is //*[@id=”kw”]. The way to obtain is to locate the element attribute of the Baidu search input box, right-click the attribute, select the "Copy" option in the pop-up shortcut menu, and select the "Copy Xpath" option in the "Copy" sub-option, such as Figure
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("http://www.baidu.com")
driver.find_element_by_xpath('//*[@id="kw"]').send_keys('Selenium')
- 5.find_element_by_link_text
LINK_TEXT is used to process hyperlinks. In the HTML code, the tag a is mainly used, and the method is find_element_by_link_text. Take clicking the "news" link on the Baidu homepage as an example to view the code corresponding to "news": <a href="http://news.baidu.com" name="tj_trnews"class="mnav">news. According to the code, you can see that it is labeled with a. The following implementation clicks on the "news" link on the Baidu homepage, and the implemented code is as follows:
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("http://www.baidu.com")
driver.find_element_by_link_text("新闻").click()