For UI automation beginners, they may not know how to start. We have installed python, selenium and other environments, how to locate elements, and how to write automated test cases? Let’s talk about xpath element positioning today.
1. F12 developer tool xpath positioning
First, we use Google to test, open the page to be tested, click F12 on the keyboard, enter the developer mode, click
, put the mouse on the element to be tested: at this point we find that it is located , right click and select copy complete xPath to get the complete path of the element: /html/body/div/div/div/div/div/div[ 2]/div[1]/div/div[1]/ul/li[1]/ul/li[3]/a/span
For example, if we want to click on the element "Get access_token", we can do this
options=webdriver.IeOptions()
options.ignore_protected_mode_settings=True
options.binary_location=r'D:\Program Files\360\360se6\Application\360se.exe'
driver=webdriver.Ie(executable_path="D:\Python\Python36\IEDriverServer.exe",options=options)
driver.get("http://10.0.0.1:9080/")
driver.find_element(By.XPATH,"/html/body/div/div/div/div/div/div[2]/div[1]/div/div[1]/ul/li[1]/ul/li[3]/a/span").click()
2. Use the xPathHelper extension program xpath to locate
The first thing to do is download the xPath Helper extension, then pin the extension
Open the webpage that needs to be tested, click to open xPath Helper, then hold down the Shift key, move the mouse, and move the mouse to the element that needs to be tested. For example, we want to find the element "get access_token", we can do this, and the following is obtained The absolute path of the element "get access_token"
If we want to click on the "get access_token" element, we can do this:
driver.find_element(By.XPATH,"/html/body[@class=' isWin']/div[@id='enterpress_app']/div[@class='frame frame_fullpage']/div[@class='assistant_frame_cnt js_scroll_cnt']/div[@class='enterpress_cnt']/div[@class='frame_main']/div[@class='frame_cnt']/div[@class='frame_cntLeft']/div[@class='frame_cntLeft_cnt']/div[@class='sidebar']/ul/li[@class='sidebarWWItem'][1]/ul/li[@class='sidebarWWItem'][3]/a[@class='sidebarWWItem_cnt']/span[@class='sidebarWWItem_title']").click()