【问题标题】:How do I trap a popup in Selenium 2 python如何在 Selenium 2 python 中捕获弹出窗口
【发布时间】:2012-01-22 07:51:15
【问题描述】:

我今晚刚开始使用 Selenium 2,所以这将是非常基本的(我希望...)。

我正在尝试通过http://contentparadise.com/ 登录我的帐户

我进入登录页面https://www.contentparadise.com/signin.aspx 并能够输入 id/pw,然后提交。但即使使用正确的 id/pw,它也会将我返回到登录页面 - 添加一个带有“发生以下错误”字样的小消息框。显然,代码中的 id/pw 是错误的,我试图将其检测为错误 - 但即使使用我的真实 id/pw,我也会得到它。

我如何检测和阅读这个消息框,为什么正确的设置没有进入主页?

我在另一个站点上使用相同的代码,使用正确的集合,它会将我带到应有的主页。

这是使用javascript登录页面的情况吗?如果您查看登录页面的来源,请查找字符串“”来启动该部分的表单。

代码如下:

from selenium import webdriver
import sys
import os

userID = "wajahbaru"
pw = "marmalade"

wdrv = webdriver.Firefox()
wdrv.get("https://www.contentparadise.com/signin.aspx")

print "Page #1 title is: " + wdrv.title; # should be "Sign In"

unamefield = wdrv.find_element_by_name("ctl00$ContentPlaceHolder1$txtUserName").send_keys(userID)
pwdfield = wdrv.find_element_by_name("ctl00$ContentPlaceHolder1$txtPassword").send_keys(pw)
pwdfield = wdrv.find_element_by_name("ctl00$ContentPlaceHolder1$btnLogin").click()

print "Page #2 title is: " + wdrv.title; # if logged in this should be "Content Paradise: Buy or Sell Software, 2D Content, 3D Models and Audio."

wdrv.get_screenshot_as_file("test.jpg")
wdrv.quit()

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    我如何检测和阅读这个消息框,为什么正确的设置没有进入主页?

    要检测错误消息框,您可以使用 id="error" 搜索元素:

    #!/usr/bin/env python
    from contextlib import closing
    from selenium.webdriver import Firefox as Browser
    from selenium.webdriver.support.ui import WebDriverWait
    
    timeout = 10 # seconds
    
    with closing(Browser()) as browser:
        browser.get('https://www.contentparadise.com/signin.aspx')
        assert browser.title == "Sign In"
        login, password, submit = map(browser.find_element_by_id,
            ['ctl00_ContentPlaceHolder1_txtUserName',
             'ctl00_ContentPlaceHolder1_txtPassword',
             'ctl00_ContentPlaceHolder1_btnLogin'])
        enter_text = lambda x, text: (x.clear(), x.send_keys(text))
        enter_text(login, "abc")
        enter_text(password, "pas$W0rd")
        submit.click()
    
        # wait for error or success
        value = WebDriverWait(browser, timeout).until(
            lambda x: ("Content Paradise" in x.title and "ok" or
                       x.find_element_by_id('error')))
        if value != "ok":
           print "error:", value.text
        browser.get_screenshot_as_file('test.jpg')
    

    【讨论】:

    • 好的,这显示了警报的文本 - 谢谢。顺便说一句,我没有收到有答案的电子邮件通知...
    【解决方案2】:

    在下面的链接中搜索“弹出对话框”。如果您想了解更多信息,请搜索您在 Google 中找到的代码,您会得到一些好东西 ;-)

    http://readthedocs.org/docs/selenium-python/en/latest/navigating.html?highlight=popup

    【讨论】:

    • 派人到谷歌不是很有帮助。 StackOverflow 是 目的地 google 派人去的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多