如何恢复“文本”?从发起警报的页面开始,esp ** **人类用户*在页面的**警告**上点击* [关闭]? [英] How do I recover the "text" from the page originating the alert, esp **after** the human user has *clicked* [dismiss] on the page's **alert**?
问题描述
基本上,当在javascript中弹出一个警告时,我可以通过调用 selenium.webdriver从
。 common.alert.Alert(browser).dismiss()。
Basically, when an alert is popped up in javascript, I can dismiss()
it from python perfectly OK, by calling selenium.webdriver.common.alert.Alert(browser).dismiss()
.
但是,如果浏览器用户通过点击<$取消警报c $ c> [确定] (在屏幕上)用鼠标,然后浏览器提醒空间迷失 body.text
可以不再从python访问。
However, if the "browser user" dismisses the alert by clicking [OK]
(on screen) with their mouse, then the browser alert gets "Lost in Space" the body.text
can no longer be accessed from python.
所以......我如何从发出警报的页面中恢复文本,esp 之后人类用户在页面的警告上点击了 [dismiss]?
So... How do I recover the "text" from the page originating the alert, esp after the human user has clicked [dismiss] on the page's alert?
以下是提示和脚本证明问题...
Here are the hints and a script to demonstrate the problem...
FYI:原始代码的目标是允许浏览器用户在屏幕上插入 在测试和手动响应特定警报。
FYI: The objective of the originating code is it allow the browser user intervene on screen in testing and manually response to specific alerts.
#!/usr/bin/env python
import os,sys,time
import selenium.webdriver
import selenium.webdriver.support.expected_conditions
print dict(python=sys.version,selenium=selenium.__version__)
path=os.path.join(os.getcwd(),"hello_worlds.html")
url="file:///"+path
open(path,"w").write("""<HTML>
<HEAD><TITLE>Head Title</TITLE></HEAD>
<BODY><H1>Hello, worlds!</H1></BODY>
</HTML> """)
browser=selenium.webdriver.Firefox()
browser.get(url)
body=browser.find_element_by_tag_name("body")
print "BODY:",body.text
try:
for enum,world in enumerate("Mercury Venus Earth Mars Asteroids Jupiter Saturn Uranus Neptune".split()):
if "Earth" in world: world+=": So do MANUALLY dismiss! {Click [OK] now!!!}"
else: world+=": AUTO PILOT... please DONT dismiss! {done via selenium.dismiss()!}"
browser.execute_script('alert("Hello, %s!")'%world)
if selenium.webdriver.support.expected_conditions.alert_is_present():
print selenium.webdriver.common.alert.Alert(browser).text
time.sleep(enum+5)
if "Earth" not in world: selenium.webdriver.common.alert.Alert(browser).dismiss()
print "BODY:",body.text
finally:
browser.quit()
输出:(地球崩溃)
{'python': '2.6.6 (r266:84292, Aug 18 2016, 15:13:37) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]', 'selenium': '2.53.2'}
BODY: Hello, worlds!
Hello, Mercury: AUTO PILOT... please DONT dismiss! {done via selenium.dismiss()!}!
BODY: Hello, worlds!
Hello, Venus: AUTO PILOT... please DONT dismiss! {done via selenium.dismiss()!}!
BODY: Hello, worlds!
Hello, Earth: So do MANUALLY dismiss! {Click [OK] now!!!}!
BODY:
Traceback (most recent call last):
File "./js_alert.py", line 37, in <module>
print "BODY:",body.text
...
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Hello, Earth: So do MANUALLY dismiss! {Click [OK] now!!!}!
Message: Unexpected modal dialog (text: Hello, Earth: So do MANUALLY dismiss! {Click [OK] now!!!}!) The alert disappeared before it could be closed.
奇怪的是,如果浏览器用户触发另一个警报(打开) 另一个页面甚至!),然后 selenium.dismiss()
将拉 body.text
从那里从
The strange thing is that if the browser user triggers another alert (on another page even!), then a selenium.dismiss()
will pull body.text
back from limbo and selenium with from then will operate as per (my) expectations.
有关如何让浏览器返回的任何建议 page.body
? (并逃避警报)
Any suggestions on how to get the browser back to the page.body
? (And escape the alert)
附录:以下是类似的问题(通过激烈搜索找到):
Addendum: Here are similar questions (found with intense searching):
- 下载 - 使用Selenium + Firefox下载文件的问题
- java alert - Selenium WebDriver - 意外模态对话框警报
- UnexpectedAlertPresentException - Webdriver错误:没有警报抛出UnexpectedAlertPresentException后
- DesiredCapabilities - 如何使用UnexpectedAlertBehaviour处理警报Selenium的功能?
- java& javascript - org.openqa.selenium.UnhandledAlertException:意外警报打开
- 禁用例外或DesiredCapabilities - org.openqa.selenium.UnhandledAlertException:意外警报打开
- 非点击建议 - < a href =https://stackoverflow.com/questions/25200476/when-i-show-a-dialog-an-exception-unhandledalertexception-modal-dialog-presen>当我显示一个对话框时,异常UnhandledAlertException :出现模态对话框被抛出
- IE和野外 - https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/4839
- java错误报告? https://groups.google.com/forum/#!topic/webdriver/ aNyOfEjMENg
- 仍然无效。捕获异常不会重置问题 - (但几乎完全相同的问题) - 如何使用python处理selenium中的javascript警报
- 建议执行
driver.findElement(By.id(myAlert));
但是会引发同样的异常 - https:/ /sqa.stackexchange.com/questions/22482/why-my-alert-is-not-popping-up -
browser.refresh() ;
给出UnexpectedAlertPresentException
所以不起作用。 https://github.com/angular/protractor/issues/1486 -
browser.switch_to.window(browser.window_handles [0])
然后body.text
给出:UnexpectedAlertPresentException
- 类似 - Python webdriver来处理不是警报的弹出浏览器窗口
- downloading - Issues downloading file using Selenium + Firefox
- java alert - Selenium WebDriver - Unexpected modal dialog Alert
- UnexpectedAlertPresentException - Webdriver error: "No alert is present" after UnexpectedAlertPresentException is thrown
- DesiredCapabilities - How to handle an Alert with "UnexpectedAlertBehaviour" capability in Selenium?
- java&javascript - org.openqa.selenium.UnhandledAlertException: unexpected alert open
- disables exception or DesiredCapabilities - org.openqa.selenium.UnhandledAlertException: unexpected alert open
- non-click suggestion - When I show a dialog, an exception "UnhandledAlertException: Modal dialog present" is thrown
- IE and in the wild - https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/4839
- java bug report? https://groups.google.com/forum/#!topic/webdriver/aNyOfEjMENg
- "still doesn't work." catching exception does not reset the problem - (However almost exactly the same problem) - how to handle javascript alerts in selenium using python
- suggested doing a
driver.findElement(By.id("myAlert"));
but that throws same exception - https://sqa.stackexchange.com/questions/22482/why-my-alert-is-not-popping-up browser.refresh();
givesUnexpectedAlertPresentException
so doesn't work. https://github.com/angular/protractor/issues/1486browser.switch_to.window(browser.window_handles[0])
thenbody.text
gives:UnexpectedAlertPresentException
- Similar - Python webdriver to handle pop up browser windows which is not an alert
推荐答案
我一直在努力解决这个问题很长一段时间;你对你的问题的评论为我解决了这个问题:
I have been struggling with this issue off and on for a long time; your comment on your question solved the problem for me:
两者之后 UnexpectedAlertPresentException
和 NoAlertPresentException抛出
...
browser.execute_script('alert("Clearing out past dialogs.")')
browser.switch_to.alert.accept()
如你所说回答,当警报出现时,webdriver正在创建一个对话框。手动关闭警报会导致其引用丢失,但它仍然阻止访问 body.text
。创建新警报似乎允许webdriver清除旧的对话框和(在接受之后)再次授予对页面的访问权。
As you said in your answer, webdriver is creating a 'dialog' when the alert is present. Closing the alert by hand causes its reference to get lost in limbo, but it's still blocking access to body.text
. Creating a new alert seems to allow webdriver to clear out that old 'dialog' and (after accepting) grants access to the page again.
这篇关于如何恢复“文本”?从发起警报的页面开始,esp ** **人类用户*在页面的**警告**上点击* [关闭]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!