Three ways of dealing with Alert in automation(Python+Webdriver)
2015-01-28 16:48
176 查看
There are 3 ways of dealing with alert in our automation (I don't think there are only 3 ways).
The method of selenium:
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.execute_script("alert(\"hello\")")
time.sleep(3)
alert = driver.switch_to_alert()
alert.accept()
Overwrite the alert of Javascript:
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
script = "window.alert = function(msg){ return true;}"
driver.execute_script(script)
driver.execute_script("alert(\"hello\")")
You will find all alert would not be pop up on this page.
Simulate keystrokes:
You have to install autopy in your python environment, and the install file at https://pypi.python.org/pypi/autopy/.
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.execute_script("alert(\"hello\")")
time.sleep(3)
autopy.key.tap(autopy.key.K_RETURN)
The method of selenium:
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.execute_script("alert(\"hello\")")
time.sleep(3)
alert = driver.switch_to_alert()
alert.accept()
Overwrite the alert of Javascript:
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
script = "window.alert = function(msg){ return true;}"
driver.execute_script(script)
driver.execute_script("alert(\"hello\")")
You will find all alert would not be pop up on this page.
Simulate keystrokes:
You have to install autopy in your python environment, and the install file at https://pypi.python.org/pypi/autopy/.
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.execute_script("alert(\"hello\")")
time.sleep(3)
autopy.key.tap(autopy.key.K_RETURN)
相关文章推荐
- How to take partial screenshot with Selenium WebDriver in python
- A database of open-source HTTP proxies written in python: WebCleaner
- HAR with Python WebDriver and BrowserMob Proxy
- Web Tier to Go With Java EE 5: Summary of New Features in JavaServer Faces 1.2 Technology
- AES encryption of files in Python with PyCrypto
- Web Tier to Go With Java EE 5: Summary of New Features in JSP 2.1 Technology
- Face Recognition with Python, in Under 25 Lines of Code
- python运行时提示WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
- Web Tier to Go With Java EE 5: Summary of New Features in Java Standard Tag Library (JSTL) 1.2
- Three ways of implementing the Singleton Pattern in Java
- Selenium with Python 008 - WebDriver 元素等待
- How to craw the Info of BiliBIli with python in Parallel
- python运行时提示WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
- python运行时提示WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
- 强参数 Strong parameters: Dealing with mass assignment in the controller instead of the model
- Running Selenium 'headless' with Chrome Driver in Python - YouTube
- HousePrice of big city with python crawl in lianjia
- Web Automation with Python for .NET
- Reading src of DriverWorks in DriverStudio 3.2