gpt4 book ai didi

python - 无法使用python在selenium中截取警告框的屏幕截图

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:48 24 4
gpt4 key购买 nike

我是 selenium 的新手,我想在弹出警报时截取警报框的屏幕截图。我写的代码如下:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoAlertPresentException
import time


class SearchXSS(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox()

def test_search_in_python_org(self):
driver = self.driver
driver.get("http://testfire.net/")
ele = driver.find_element_by_name("txtSearch")
ele.send_keys("<script>alert(document.cookie)</script>")
ele.send_keys(Keys.RETURN)
try:
time.sleep(1)
alert = driver.switch_to_alert()
driver.get_screenshot_as_file('screenshot.png')
alert.accept()
except NoAlertPresentException as e:
print "no alert to accept "
fo.close()


def tearDown(self):
self.driver.quit()

if __name__=="__main__":
unittest.main()

我在截屏的时候遇到了这个问题,报错信息如下

====================================================================== ERROR: test_search_in_python_org (main.SearchXSS) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/user/programs/sele/testx.py", line 22, in test_search_in_python_org driver.get_screenshot_as_file('screenshot.png') File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 798, in get_screenshot_as_file png = self.get_screenshot_as_png() File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 817, in get_screenshot_as_png return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 827, in get_screenshot_as_base64 return self.execute(Command.SCREENSHOT)['value'] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 191, in check_response raise exception_class(message, screen, stacktrace, value['alert'].get('text')) UnexpectedAlertPresentException: Alert Text: amSessionId=92632533494 Message: Unexpected modal dialog (text: amSessionId=92632533494) Stacktrace: at fxdriver.modals.closeUnhandledAlert/< (file:///tmp/tmpdnEJbt/extensions/fxdriver@googlecode.com/components/prompt-service.js:4745) at (http://testfire.net/search.aspx?txtSearch=%3Cscript%3Ealert%28document.cookie%29%3C%2Fscript%3E:80)

---------------------------------------------------------------------- Ran 1 test in 10.346s

FAILED (errors=1) [Finished in 10.4s with exit code 1]

帮我解决这个问题。

最佳答案

你不能用alert框截取screenshot,你需要先处理alert意味着accept或者dismiss 然后去截屏,因为当 selenium 去截取带有警报的屏幕截图时,它总是会抛出 UnexpectedAlertPresentException无法截取使用 seleniumalert 框截图。所以你需要做如下:-

alert = driver.switch_to_alert()
alert.accept()
driver.get_screenshot_as_file('screenshot.png')

如果你想使用 alert 截屏,你应该尝试使用其他库的一些不同方法,在 java 中有 Robot 类可以截取 screenshot 在这种情况下,但我不确定 python 中的等效项是什么。

关于python - 无法使用python在selenium中截取警告框的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38719337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com