一.文件上传
input标签可以直接用send_keys(文件地址)上传文件
用法:
- 先定位元素
- send_keys('文件路径+文件名')
from time import sleep
from selenium_frame_window.base import Base
class TestFile(Base):
def test_file_upload(self):
self.driver.get("https://image.baidu.com/")
self.driver.find_element_by_xpath('//*[@id="sttb"]/img[1]').click()
self.driver.find_element_by_id("stfile").send_keys("C:/Users/pythonProject3/selenium_file/img/it_beijing-003.jpg")
sleep(3)
二.弹框处理
弹框类型:alert、confirm、prompt 弹框
操作弹框
常用方法
- switch_to.alert():获取当前页面的警告框
- text:返回alert、confirm、prompt 弹框中文字信息
- accept():接受现有警告框,点击确定
- dismiss():解散现有警告框,点击取消
- send_keys():发送文本至告警框
from time import sleep
from selenium.webdriver import ActionChains
from selenium_frame_window.base import Base
class TestFile(Base):
def test_alert(self):
self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
self.driver.switch_to.frame("iframeResult")
drag_ele = self.driver.find_element_by_id("draggable")
drop_ele = self.driver.find_element_by_id("droppable")
action = ActionChains(self.driver)
action.drag_and_drop(drag_ele,drop_ele).perform()
print("点击alert 确认")
self.driver.switch_to.alert.accept()
self.driver.switch_to.default_content()
self.driver.find_element_by_id('submitBTN').click()
sleep(3)