一、文件上传操作-input标签文件选择
driver.find_element(By.ID,"file‐upload").send_keys("路径/selenium‐snapshot.jpg")
二、文件上传操作-非input标签文件选择
PyAutoGui 库来解决上传问题
'点击上传'
ActionChains(driver).click(el).perform()
'移动到,地址栏,需要先定位'
pyautogui.moveTo(3360, 78)
sleep(2)
'点击'
pyautogui.click()
'删掉原来的路径'
pyautogui.press('backspace')
'键盘输入,文件存储的地址'
pyautogui.write(r"/Volumes/attached/huace_edu/vipCourse/WebUI_automation/web_new_code/class_07/")
'按enter,这里要按2下才生效'
pyautogui.press('enter')
pyautogui.press('enter')
'移动鼠标'
pyautogui.moveTo(3406, 255)
'点击上传文件'
pyautogui.click()
'移动鼠标'
pyautogui.moveTo(3804, 614)
'点击,打开按钮'
pyautogui.click()
'点击网页中的upload'
driver.find_element(By.ID, "file-submit").submit()
三、日期控件
3.1 可输入日期控件
一般的日期控件是可以直接输入日期的,如果使用webdriver 去设置日期,
- 定位到该日期控件
- 使用sendKeys 方法
'定位到日期控件,并输入'
20 locator = (By.XPATH,'//input[@id="test1"]')
21 wait.until(ec.visibility_of_element_located(locator)).send_keys("2022‐09‐30")
3.2 readonly日期控件(很少有使用)
这个时候,没法调用WebElement的sendKeys()
方法1:可以使用JS remove readonly attribute,然后sendKeys
js = 'document.getElementByName("establish_time").removeAttribute("readonly")'
driver.execute_script(js)
'在sendKeys之前应该清空原来的默认值'
driver.find_element(By.NAME,"establish_time").clear()
driver.find_element(By.NAME,"establish_time").send_keys("2033‐08‐03")
方法2:使用js方法,输入日期,直接改掉输入框元素的值即可
'去掉元素的readonly属性'
js = 'document.getElementByName("establish_time").removeAttribute("readonly");'
driver.execute_script(js)
'使用js方法输入日期'
js_value = 'document.getElementByName("establish_time").value = "2020‐05‐28"'
driver.execute_script(js_value)
3.3 非readonly,也无法直接输入的日期控件
用到Selenium4的滚轮
'修改分钟,改成59'
locater = (By.XPATH,"//p[contains(text(),'分')]/../ol/li[60]")
'基于元素,按给点数值滚动'
min = driver.find_element(By.XPATH, "//p[contains(text(),'分')]/../ol")
'定义滚动起始元素'
scroll_origin = ScrollOrigin.from_element(min)
min59 = driver.find_element(*locater)
'按住不动,滚动指定数值成功'
ActionChains(driver) \
.scroll_to_element(min)\
.click_and_hold(min)\
.scroll_from_origin(scroll_origin,0,1800) \
.release(min)\
.move_to_element(min59)\
.click()\
.perform()
如果ActionChains不能解决问题,还可以考虑使用PyAutoGui,用坐标进行定位并点击