Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Selenium与Python 3_Python 3.x_Selenium - Fatal编程技术网

Python 3.x Selenium与Python 3

Python 3.x Selenium与Python 3,python-3.x,selenium,Python 3.x,Selenium,试图利用“def assert_alert_present():函数在出现警报时自动执行断言。想在沃尔玛网站的“注册”按钮上使用它,默认情况下,它是这样的: 您的密码必须包含6到12个字符,不带空格。请再试一次 我故意使用少于6个或多于12个字符,然后出现警报。不确定如何为该警报编写函数,以便在警报存在时通过,而在警报不存在时失败 希望这就是你想要的: from selenium import webdriver from selenium.webdriver.common.by import

试图利用“def assert_alert_present():函数在出现警报时自动执行断言。想在沃尔玛网站的“注册”按钮上使用它,默认情况下,它是这样的:

您的密码必须包含6到12个字符,不带空格。请再试一次


我故意使用少于6个或多于12个字符,然后出现警报。不确定如何为该警报编写函数,以便在警报存在时通过,而在警报不存在时失败

希望这就是你想要的:

from selenium import webdriver
from selenium.webdriver.common.by import By


def assert_alert_present():
    driver = webdriver.Chrome()
    driver.maximize_window()
    baseurl = "https://www.walmart.com/account/signup"
    driver.get(baseurl)

    driver.find_element_by_name("firstName").send_keys("Vasa")
    driver.find_element_by_name("lastName").send_keys("Pupkin")
    driver.find_element_by_name("email").send_keys("abc@123.com")
    driver.find_element_by_name("password").send_keys("123")

    #this will check and verify the alertpopup
    try:
        assert driver.find_element(By.ID, "password-help")
        print "Alert is present"
    except:
        print "Alert is not present"

    driver.find_element_by_css_selector("button.l-margin-top").click()

    errormessage = driver.find_element_by_css_selector('.error-label').text

    if errormessage.strip() == "Your password must contain between 6 and 12 characters, with no spaces. Please try again.":
        print "Error lable is present there "
    else:
        print "Error lable is not present on website, Please check the website "

assert_alert_present()
这将打印消息:

Alert is present
Error lable is present there 

分享你的代码,你所拥有的驱动程序。按名称(“名字”)查找元素。发送密钥(“Vasa”)驱动程序。按名称(“姓氏”)查找元素。发送密钥(“Pupkin”)驱动程序。按名称(“电子邮件”)查找元素。发送密钥(“VasaPupkin@gmail.com)驱动程序。按名称(“密码”)查找元素。发送密钥(“VASAUPKIN1234”)驱动程序。按id(“注册提交btn”)查找元素。单击()#def assert_alert_present():此时我是@page此测试的目标是断言警报消息存在:“您的密码必须包含6到12个字符,不带空格。请重试。”不确定确切的语法和使用步骤:'def assert_alert_present():函数谢谢。它运行得很好。只是需要澄清一下。我很难通过id找到警报元素“assert driver.find_元素(by.id,“password help”)”。当我点击那个灰色警报时,它就消失了。在输入“123”密码后,在“Password”字段下查看相同的红色错误消息时,我找到了对它的唯一引用。它在一个大教堂的顶部。感恩节快乐!感恩节快乐。div class=“validation group”data reactid=“.0.0.1.0.1.3.4.1.0.1”>您的密码必须介于6到12个字符之间。这是弹出窗口的html