python selenium 弹出框处理的实现
弹出框有两种:页面弹出框(可定位元素能操作)、Windows弹出框(不能直接定位)
一、页面弹出框
等待弹出框出现之后,定位弹出框,操作其中元素
如:
driver=webdriver.Chrome() driver.get("https://www.baidu.com") driver.maximize_window() #点击百度登录按钮 driver.find_element_by_xpath('//*[@id="u1"]//a[@name="tj_login"]').click() #等待百度登录弹出框中要出现的元素可见 ele_id="TANGRAM__PSP_10__footerULoginBtn" param=(By.ID,ele_id) #元素可见时,再进行后续操作 WebDriverWait(driver,10).until(EC.visibility_of_element_located(param)) driver.find_element_by_id(ele_id).click() time.sleep(5) driver.quit()
二、Windows弹出框
使用driver.switch_to.alert 切换到Windows弹出框
Alert类提供了一系列操作方法:
- accept()确定
- dismiss()取消
- text()获取弹出框里面的内容
- send_keys(keysToSend)输入字符串
如:
#1:定位alert弹出框 #点击页面元素,触发alert弹出框 driver.find_element_by_xpath('//*[@id="alert"]').click() time.sleep(3) #等待alert弹出框可见 WebDriverWait(driver,20).until(EC.alert_is_present()) #从html页面切换到alert弹框 alert=driver.switch_to.alert #获取alert的文本内容 print(alert.text) #接受--选择“确定” alert.accept() #2:定位confirm弹出框 driver.find_element_by_xpath('//*[@id="confirm"]').click() time.sleep(3) WebDriverWait(driver,20).until(EC.alert_is_present()) alert=driver.switch_to.alert print(alert.text) #接受--选择“取消” alert.dismiss() #3:定位prompt弹出框 driver.find_element_by_id("prompt").click() time.sleep(3) WebDriverWait(driver,20).until(EC.alert_is_present()) alert=driver.switch_to.alert alert.send_keys("jaja") time.sleep(5) print(alert.text) #alert.dismiss() alert.accept()
实例
首先复制下列的html代码,保存为test.html到与脚本相同的文件夹下。这个html文件包含三个按钮,点击后会弹出三种不同的弹出框,另外还有一个文字区域,显示刚才的动作。
alert,confirmandprompt functionmyFunctionAlert(){ window.alert('thisisanalert,ithasaconfirmbutton') document.getElementById('action').value='youjustclickedconfirmbuttonofalert()' } functionmyFunctionConfirm(){ varresult=window.confirm('thisisaconfirm,ithasaconfirmbuttonandacancelbutton') if(result==true){ document.getElementById('action').value='youjustclickedconfirmbuttonofconfirm()' }elseif(result==false){ document.getElementById('action').value='youjustclickedcancelbuttonofconfirm()' }else{ document.getElementsById('action').value='youdidnothing' } } functionmyFunctionPrompt(){ varresult=window.prompt('thisisaprompt,ithasaninputandtwobuttons') if(result==null){ document.getElementById('action').value='youjustclickedcancelbuttonofprompt()' }elseif(result==''){ document.getElementById('action').value='youjustinputnothingandclickedconfirmbuttonofprompt()' }else{ document.getElementById('action').value='youjustinput\''+result+'\'andclickedconfirmbuttonofpromt()' } }
showalert
showconfirm
showprompt
首先我们先实现:
1.点击第一个按钮‘showalert',然后在弹出的对话框中点击【确认】按钮,并且打印你的动作。
2.点击第二个按钮‘showconfirm',然后在弹出的对话框中点击【取消】按钮,并且打印你的动作。
#-*-coding:utf-8-*- fromseleniumimportwebdriver fromtimeimportsleep importos driver=webdriver.Chrome() driver.implicitly_wait(10) file='file:///'+os.path.abspath('test.html') driver.get(file) driver.find_element_by_css_selector('body>button:nth-child(2)').click()#使用css选择器定位,showalert按钮为body下的第二个子元素 sleep(2) alert=driver.switch_to.alert#切换到alert print('alerttext:'+alert.text)#打印alert的文本 alert.accept()#点击alert的【确认】按钮 print('whatyouhavedoneis:'+driver.find_element_by_id('action').get_attribute('value'))#打印刚才的操作(获取页面最下方的textarea中文本) sleep(2) driver.find_element_by_css_selector('body>button:nth-child(4)').click() sleep(2) confirm=driver.switch_to.alert print('confirmtext:'+confirm.text)#打印confirm的文本 confirm.dismiss()#点击confirm的取消按钮 print('whatyouhavedoneis:'+driver.find_element_by_id('action').get_attribute('value')) sleep(2) driver.quit()
接着我们来操作:点击第三个按钮‘showprompt',输入文字后点击【确认】按钮。
#-*-coding:utf-8-*- fromseleniumimportwebdriver fromselenium.webdriver.common.alertimportAlert#导入Alert包 fromtimeimportsleep importos driver=webdriver.Chrome() driver.implicitly_wait(10) file='file:///'+os.path.abspath('test.html') driver.get(file) driver.find_element_by_css_selector('body>button:nth-child(6)').click() sleep(2) prompt=Alert(driver)#实例Alert对象,但使用时前面一定要导入Alert包 print('prompttext:'+prompt.text)#打印promt的文言 prompt.send_keys('testprompt')#发送信息到输入框中 sleep(2) prompt.accept()#点击【确认】按钮 print('whatyouhavedoneis:'+driver.find_element_by_id('action').get_attribute('value'))#打印刚才的操作 sleep(2) driver.quit()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。