2011-12-25 80 views
14

我刮使用硒的webdriver的Python点击JavaScript弹出通过webdriver的

我的工作网页上的网页,有一个表格。我可以填写表格,然后点击提交按钮。

它生成一个弹出窗口(Javascript警报)。我不确定,如何通过webdriver点击弹出窗口。

任何想法如何做到这一点?

感谢

+0

参见http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Does_WebDriver_support_Javascript_alerts_and_prompts?这不是Python,但我认为这是可以理解的。 – rubik 2011-12-25 20:54:32

+0

雅。但它不适用于Python。我一直无法找到一个等价的函数来处理弹出窗口。 – Kiran 2011-12-25 21:18:49

+0

[如何点击并验证是否存在弹出窗口(警告)]的可能重复(http://stackoverflow.com/questions/3084850/how-to-click-and-verify-the-existence-of-a -pop-up-alert) – Acorn 2011-12-25 23:53:22

回答

0

取决于处理表单提交 如果没有这样的功能,请尝试使用后

+0

我收到一封简单的javascript警告,声明“感谢您提交”,我只想按回车关闭弹出窗口。 – Kiran 2011-12-25 21:19:35

1

提交表单我使用Ruby绑定,但在这里我在硒的Python中的JavaScript功能绑定2文档: http://readthedocs.org/docs/selenium-python/en/latest/index.html

Selenium WebDriver内置支持处理弹出对话框。当您已经triggerd和行动,将打开一个弹出窗口,你可以用下面的访问警报:

alert = driver.switch_to_alert() 

现在,我想你可以做这样的事情:

if alert.text == 'A value you are looking for' 
    alert.dismiss 
else 
    alert.accept 
end 

希望它能帮助!

19

的Python脚本的webdriver:

from selenium import webdriver 

browser = webdriver.Firefox() 
browser.get("http://sandbox.dev/alert.html") 
alert = browser.switch_to_alert() 
alert.accept() 
browser.close() 

网页(alert.html):

<html><body> 
    <script>alert("hey");</script> 
</body></html> 

运行脚本的webdriver将打开HTML页面,显示一个警告。 Webdriver立即切换到警报并接受警报。 Webdriver然后关闭浏览器并结束。

如果你不确定会有警报,那么你需要用类似的东西来捕捉错误。

from selenium import webdriver 

browser = webdriver.Firefox() 
browser.get("http://sandbox.dev/no-alert.html") 

try: 
    alert = browser.switch_to_alert() 
    alert.accept() 
except: 
    print "no alert to accept" 
browser.close() 

如果您需要检查警报的文字,你可以通过访问警报对象的文本属性得到警报的文本:

from selenium import webdriver 

browser = webdriver.Firefox() 
browser.get("http://sandbox.dev/alert.html") 

try: 
    alert = browser.switch_to_alert() 
    print alert.text 
    alert.accept() 
except: 
    print "no alert to accept" 
browser.close() 
+1

switch_to_alert()现已在最新的Selenium Python绑定v 2.46.0中被弃用。改为使用driver.switch_to.alert。资料来源:http://selenium-python.readthedocs.org/en/latest/api.html – SpartaSixZero 2015-07-01 20:55:40

1

如果你想接受或点击弹出窗口,不管它是什么,然后

alert.accept 

哪里 alertselenium.webdriver.common.alert.Alert(driver)类 和0的对象是该对象的方法

Source

4
from selenium import webdriver 
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Firefox() 
#do something 
if EC.alert_is_present: 
    print "Alert Exists" 
    driver.switch_to_alert().accept() 
    print "Alert accepted" 
else: 
    print "No alert exists" 

进一步了解excepted_conditions https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

+0

仅供参考,switch_to_alert()现在已被弃用,并已被替换为switch_to.alert http:// selenium-python。 readthedocs.org/en/latest/api.html#selenium.webdriver.remote.webdriver.WebDriver.switch_to_alert – demouser123 2015-06-21 07:01:59

+0

@bad_deadpool感谢您的更新!此外:该链接是404 - 该文档现在位于http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.switch_to_alert – 2017-05-08 22:36:51

+0

感谢@GregSadetsky的更新。 – 2017-05-10 03:04:24