做一元云购网站,建个商城网站多少钱,国外二级域名免费申请,做网站公司需要提供的资料场景页面上弹出的对话框是自动化测试经常会遇到的一个问题。很多情况下这个弹出的对话框是一个iframe#xff0c;处理起来有点麻烦#xff0c;需要进行switch_to操作. 请参考文章测试曾哥#xff1a;[Selenium]23.对话框的处理zhuanlan.zhihu.com但现在很多前端框架的对话…场景页面上弹出的对话框是自动化测试经常会遇到的一个问题。很多情况下这个弹出的对话框是一个iframe处理起来有点麻烦需要进行switch_to操作. 请参考文章测试曾哥[Selenium]23.对话框的处理zhuanlan.zhihu.com但现在很多前端框架的对话框都是div形式的这就让我们的处理变得十分简单了。处理对话框一般会做下面的一些事情打开对话框关闭对话框操作对话框中的元素代码modal.htmlhtmlheadmeta http-equivcontent-type contenttext/html;charsetutf-8 /titlemodal/title script typetext/javascript async srchttp://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js/scriptlink hrefhttp://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css relstylesheet / script typetext/javascript$(document).ready(function(){$(#click).click(function(){$(this).parent().find(p).text(try watir-webdriver right now!);});});/script/headbodyh3modal/h3div classrow-fluiddiv classspan6 !-- Button to trigger modal --a href#myModal rolebutton classbtn btn-primary data-togglemodal idshow_modalClick/a!-- Modal --div idmyModal classmodal hide fade tabindex-1 roledialog aria-labelledbymyModalLabel aria-hiddentruediv classmodal-headerbutton typebutton classclose data-dismissmodal aria-hiddentrue×/buttonh3 idmyModalLabelModal header/h3/divdiv classmodal-bodypwatir-webdriver is better than slenium-webdriver/pa href# idclickclick me/a/divdiv classmodal-footerbutton classbtn data-dismissmodal aria-hiddentrueClose/buttonbutton classbtn btn-primarySave changes/button/div/div/div /div /bodyscript srchttp://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js/script/htmlmodal.py # -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
import selenium.webdriver.support.ui as ui
if HTTP_PROXYin os.environ: del os.environ[HTTP_PROXY]dr webdriver.Chrome()
file_path file:/// os.path.abspath(modal.html)dr.get(file_path)# 打开对话框
dr.find_element_by_id(show_modal).click()wait ui.WebDriverWait(dr, 10)
wait.until(lambda dr: dr.find_element_by_id(myModal).is_displayed())# 点击对话框中的链接
# 由于对话框中的元素被蒙板所遮挡直接点击会报 Element is not clickable的错误
# 所以使用js来模拟click
# 在watir-webdriver中只需要fire_event(:click)就可以了
link dr.find_element_by_id(myModal).find_element_by_id(click)
dr.execute_script($(arguments[0]).click(), link)
sleep(2)# 关闭对话框
buttons dr.find_element_by_class_name(modal-footer).find_elements_by_tag_name(button)
buttons[0].click()dr.quit()