How to slide the page to the bottom?
Generally speaking, when doing web automation testing, there is no need to write code separately, and slide the page to be visible, because the click operation, as long as the element exists and is loaded, you can click it, without writing additional code for sliding elements.
If you need to slide in special circumstances, the execute_script method in the selenium library in python executes the js statement to implement the scroll function
'arguments [element object] .scrollIntoView ();', element object
driver.execute_script ('arguments [0] .scrollIntoView (false);', ele) #Move the element to the bottom to be visible driver.execute_script ('arguments [0] .scrollIntoView ();', ele) #Move the element to the top to be visible driver.execute_script ('window.scrollTo (0, document.body.scrollHeight)') # Move the page to the bottom of the page driver.execute_script ("window.scrollTo (document.body.scrollHeight, 0)") # Move the page to the top of the page
Date box processing
I believe you have encountered a date box when testing a web page. It is a type that can only be selected and cannot be entered directly. How to deal with this kind of web automation test?
Change the attribute value so that it can be entered directly
from selenium import webdriver driver = webdriver.Chrome() # pha_js = "var a = arguments[0];a.readOnly=false;a.value= arguments[1];"#js语法 loc = ("xpath","//*[@id='train_date']") ele = driver.find_element(*loc) now_10 = "1111" # datetime to get the current time driver.execute_script("var a = arguments[0];a.readOnly=false;a.value= arguments[1];",ele,now_10)
12306 Example of date selection box
driver=webdriver.Chrome() driver.get('https://www.12306.cn/index/') driver.implicitly_wait(20) #Select the starting point first, then modify the starting point driver.find_element_by_id("fromStationText").click() driver.find_element_by_xpath('//li[@title="北京"]').click() pha_js='var a=document.getElementById("fromStationText");a.value="深圳"' driver.execute_script(pha_js) #Select the destination first, then modify the destination driver.find_element_by_id("toStationText").click() driver.find_element_by_xpath('//li[@title="上海"]').click() arive_js='var a=document.getElementById("toStationText");a.value="常德"' driver.execute_script(arive_js) #Date processing time_js='var a=document.getElementById("train_date");a.readOnly=false;a.value="2020-05-01"' driver.execute_script(time_js) #Date processing ele4=(By.ID,'search_one') driver.find_element(*ele4).click()
Upload operation
Web automated testing, how to simulate uploading files
Due to the upload operation, which is not a web interface, you cannot use the selenium library in python
Only use other libraries
1. Install pip install pypiwin32 library first
# 1 \ Find the input box and open button element; 2. Enter the address and click open. # Premise: The windows upload window has appeared. Sleep1-2 seconds to wait for the pop-up to appear. def upload(filePath,browser_type="chrome"): if browser_type == "chrome": title = "Open" else: title = "" #Find element # 级 窗 "# 32770", "Open" dialog = win32gui.FindWindow("#32770",title) # ComboBoxEx32 = win32gui.FindWindowEx(dialog,0,"ComboBoxEx32",None) #二级 comboBox = win32gui.FindWindowEx(ComboBoxEx32,0,"ComboBox",None) #三级 #Edit button edit = win32gui.FindWindowEx (comboBox, 0, 'Edit', None) #Four levels #Open button button = win32gui.FindWindowEx (dialog, 0, 'Button', "& Open") # Level 2 #To edit, enter the file path. win32gui.SendMessage (edit, win32con.WM_SETTEXT, None, filePath) #Send file path win32gui.SendMessage (dialog, win32con.WM_COMMAND, 1, button) #Click the open button