Table of contents
2. Operation method of the drop-down box
4. Attachment upload operation method
2. Use pywinauto (only available on the window platform)
1. Time waiting method
1. Forced waiting
Import timed wait library
from time import sleep
#或者
import time
time.sleep(10) # means to forcibly wait for 10s. When the next sentence of code is executed, this waiting method executes the next statement when the time is up, but there is no guarantee that the element is actually loaded during the waiting time.
2. Implicit waiting
driver.implicitly_wait(30) #等待30s
It means that all the elements of the page are loaded within the specified time and then execute the next step, otherwise wait until the time expires before continuing to the next step.
Disadvantage: The required elements have been loaded, but the page has not been loaded yet, so you need to continue to wait for the page to be loaded before performing the next step.
3. Explicit wait
from selenium.webdriver.support.wait import WebDriverWait #导入显性等待包
WebDriverWait, in conjunction with this class, has until() and until_not() methods, which means that the program will judge whether the specified element is loaded every x seconds, and then execute the next step after loading, otherwise continue to judge every x seconds, and specify the time due. An exception is thrown if it times out.
WebDriverWait(driver, 10,0.5).until(driver.find_element( By.CSS_SELECTOR,""))
2. Operation method of the drop-down box
from selenium.webdriver.support.select import Select#导包
se=driver.find_element(By.ID, 'provise')#定位到元素
select=Select(se)#实例化
select.select_by_index(1)#通过索引选择下拉元素
select.select_by_value('bj')#通过下拉元素的value选择下拉元素
select.select_by_visible_text('上海')#通过下拉元素的文本内容选择下拉元素
In addition, some other less commonly used ones are:
- dselect_by_value() : reverse selection based on value
- dselect_by_index() : reverse selection based on index
- dselect_by_visible_text() : Reverse selection based on text
- dselect_all(): deselect all
- opitions: all options
- all_selected_options: all selected options
- first_selected_option : the first selected option
3.Alert pop-up box method
alert/confim/prompt, where
- alert window: Prompt user information only confirm button
- Confirm window: with confirm and cancel buttons
- Prompt window: There are input boxes, confirmation and cancel buttons
text: Return (get) the text information in alert/confirm/prompt
accept(): accept the existing alert box
dismiss(): discard the existing warning box
send_keys(keys_ToSend): Send text to the alert box
#切换到alert
alert = self.driver.switch_to.alert
print(alert.text)
sleep(2)
alert.accept()
#confirm弹框
self.driver.find_element_by_id('confirm').click()
confirm = self.driver.switch_to.alert
print(confirm.text)
sleep(3)
#确定
# confirm.accept()
#取消
confirm.dismiss()
#prompt弹框
self.driver.find_element_by_id('prompt').click()
prompt = self.driver.switch_to.alert
print(prompt.text)
prompt.send_keys('20')
sleep(3)
prompt.accept()
4. Attachment upload operation method
1. If the element label is input, you can directly input the file address with the send_keys method to upload the file
ele=driver.find_element_by_id("id")
ele.send_keys(r"c:\xxx.txt")
2. Use pywinauto (only available on the window platform)
pip install pywinauto
from pywinauto.keyboard import send_keys
#找到元素,click(),调出文件上传的系统弹框界面
ele=driver.find_element_by_id("id")
ele.click()
time.sleep(2)
#输入文件路径
send_keys("文件路径")
#回车,上传文件
send_keys("{VK_RETURN}")
3. Cross-platform upload
使用pyautogui跨平台
pip install pillow==6.2.2
pip insall pyautogui
pyautogui.write("d:xxx.txt")
pyautogui.press("enter")
pyperclip.copy(r"d:\用户\文件.txt")#文件路径
time.sleep(2)
pyautogui.hotkey("ctrl","v")#粘贴
pyautogui.press("enter",presses=2)#输入两次enter键,防止出错