selenium 文件上传 弹框处理
文件上传
input 标签可以直接使⽤ send_keys(⽂件地址) 上传⽂件
⽤法:
- ele = driver.find_element_by_id('上传按钮id')
- ele.send_keys('⽂件路径+⽂件名')
打开搜狗图片上传文件:
def test_image(self):
self.driver.get('https://pic.sogou.com/')
self.driver.find_element(By.ID, 'cameraIco').click()
self.driver.find_element(By.XPATH, '//*[@id="identifyPop"]/div[2]/div[1]/input').send_keys(r'C:\\Users\\user\\Desktop\\123.png')
time.sleep(5)
弹框处理
alert、confirm 和 prompt 弹框,div 模态框
教程:https://huilansame.github.io/archivers/switch-to-alert-window-div
操作alert弹框常用的方法:
-
switch_to.alert():获取当前页⾯上的警告框。
-
text:返回alert/confirm/prompt 中的⽂字信息。
-
accept():接受现有警告框(点击"确定"或者"是")
-
dismiss():解散现有警告框(点击"否"或者"取消")
-
send_keys(keysToSend):发送⽂本⾄警告框,keysToSend:将⽂本发送⾄警告框
测试网站:https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable

把右侧元素1拖拽到元素2,点击alert弹框的‘确定’,再点击‘点击运行’
def test_alert(self):
self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
self.driver.switch_to.frame('iframeResult')
ele1 = self.driver.find_element(By.ID, 'draggable')
ele2 = self.driver.find_element(By.ID, 'droppable')
action = ActionChains(self.driver)
action.drag_and_drop(ele1, ele2).perform()
time.sleep(5)
self.driver.switch_to.alert.accept()
self.driver.switch_to.default_content()
self.driver.find_element(By.ID, 'submitBTN').click()
time.sleep(5)

浙公网安备 33010602011771号