In the automated test, Selenium is still simple and easy to learn. In order to help her girlfriend do a repetitive work, just as she was learning, she tried to help her do it first. At the beginning, she just touched the stone and crossed the river to gain experience in practice. She turned over her book on automated testing and learned some basic usage. There is not much research on the construction of multi browser synchronous collaboration, mouse events and integrated construction tools.
The following points should be noted:
1. There are two situations to consider when the element cannot be located:
① Dynamic id cannot locate element
#The div here varies according to different peer subscripts print("Ready to get the element, num_list_index by",self.current_dealWith) el = self.driver.find_element_by_xpath(f'//*[@id="layui-layer{self.current_dealWith}"]').find_element_by_xpath( f'//*[@id="layui-layer{self.current_dealWith}"]/div[2]')
The value of {self.current_dealWith} changes dynamically with the increase of access elements
② iframe reason cannot locate element
def turnToTableFram(self): print(f'Exiting to default frame') self.driver.switch_to.default_content() print(f'About to enter bottomFrame') self.driver.switch_to.frame("bottomFrame") print(f'get into bottomFrame') print(f'About to enter op_frame') # driver.switch_to.frame("op_frame") el = self.driver.find_element_by_id('op_frame') print(f'get into op_frame') frame = el.find_element_by_name('mainFrame') print(frame) self.driver.switch_to.frame(frame) print(f'get into mainFrame') # Get tablewebElement and call method table = self.driver.find_element_by_xpath('//*[@id="ngView"]/gt-container/div/div/div/div[1]/table') return table
When there are iframe and frame in the page, you need to look at the label level, enter layer by layer from the outermost frame, and the key statement is "self" driver. switch_ to. frame("bottomFrame")
Note: frameSet can be skipped without accessing the frame. If the frame in the frameSet cannot be located and the id changes dynamically, you can observe whether the required element is always in the frame of a specific position. If so, you can access it by subscript.
③. Click too fast. If the page is not loaded, you need to click on the elements on the page
There are several ways, time Sleep (time), display wait, implicit wait
Wrong positioning mode and wrong writing of xpath statement may lead to failure of positioning
When selenium is in use, it mainly uses ① switch frames or focused pages, and see each frame and alert as a new web page. When locating elements, you should know which page you are going to; ② The second major function is positioning. There are many ways of positioning, so there are many ways to realize it. xpath, class, ID, attribute, tagName, etc. ③ all the elements located will return a tag object of WebElement when find is used_ element_ When XXX, the label object is unique, and can take text and tag_name and other methods that are not available in the book, such as taking its attribute get_attribute('id')
current_element = self.driver.switch_to_active_element() print('current_element3',current_element.tag_name,current_element.text,current_element.get_attribute('id')) #The second div currently in the pop-up box
④ if find is used_ elements_ XXX, when the page is not refreshed (clicking on the event may lead to refresh), you can use for traversal to operate on each label object. However, as long as the DOM field changes and these label objects cannot be found, each item of for traversal cannot perform the above operations on it. To solve this problem, you need to relocate to the superior element and retrieve the list again.
def for_element(self): #Count the number of current pages that do not need to be processed WebElem_table = log.turnToTableFram() #Because after operating a piece of data, as long as the page has jump and refresh, the object obtained in the page dom will change. The following methods cannot be used print('WebElem_table') print(WebElem_table.size,WebElem_table.tag_name) # # Get each row element in table table_tr_list = WebElem_table.find_elements_by_tag_name('tr') print(table_tr_list) # for tr in table_tr_list: print(f"original table Zhongyou{len(table_tr_list)}element") for num_list_index in range(1,len(table_tr_list)): #Modifying this can detect whether the page is normal after executing the single item. # for num_list_index in range(1,2): # Correction page log.changePage() #Reload the table element that exists in the dom domain WebElem_table = log.turnToTableFram() table_tr_list = WebElem_table.find_elements_by_tag_name('tr') print(f"table_tr_list current table Zhongyou{len(table_tr_list)}element") print(f"num_list_index Currently ready to process section{num_list_index}Elements") print(table_tr_list[num_list_index].size,table_tr_list[num_list_index].tag_name)
The functions corresponding to other small points and specific codes are not described in detail, and there are detailed documents https://www.selenium.dev/selenium/docs/api/py/api.html
Unofficial Chinese documents: https://python-selenium-zh.readthedocs.io/zh_CN/latest/