如何使用Selenium处理html5约束验证弹出窗口? [英] How to handle html5 constraint validation pop-up using Selenium?

查看:137
本文介绍了如何使用Selenium处理html5约束验证弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片说明了一切. 单击登录"按钮后,请填写此字段".出现弹出消息.它看起来像一些JavaScript的东西. 我想使用Selenium获取此弹出消息的文本. 可能吗?这是电子邮件输入字段的html:

The picture says it all. After click on "LOG IN" button, "Please fill out this field." pop-up message appears. It looks like some javascript thing. I want to get text of this pop-up message by using Selenium. Is it even possible ? Here is the html of email input field:

<input autocorrect="none" autocapitalize="none" spellcheck="false" autofocus="autofocus" class="cell small-21 form-text required" data-drupal-selector="edit-name" aria-describedby="edit-name--description" type="text" id="edit-name" name="name" value="" size="60" maxlength="60" required="required" aria-required="true" placeholder="Email or Username">

P.S.当弹出消息出现时,然后在电子邮件字段html附近显示的浏览器开发工具中的事件"指示.

P.S. When pop-up message appears, then "event" indication in browser dev-tools shown near email field html.

推荐答案

您所指的弹出窗口

The popup which you are referring is the outcome of Constraint API's element.setCustomValidity() method.

注意:HTML5约束验证不会消除服务器端验证的需要.即使可以预期的无效表单请求的数量要少得多,无效请求仍然可以由不兼容的浏览器(例如,没有HTML5和JavaScript的浏览器)或试图欺骗您的Web应用程序的坏人发送.因此,与HTML4一样,您还需要在服务器端验证输入约束,方法与在客户端进行的验证一致.

Note: HTML5 Constraint validation doesn't remove the need for validation on the server side. Even though far fewer invalid form requests are to be expected, invalid ones can still be sent by non-compliant browsers (for instance, browsers without HTML5 and without JavaScript) or by bad guys trying to trick your web application. Therefore, like with HTML4, you need to also validate input constraints on the server side, in a way that is consistent with what is done on the client side.

解决方案

要检索从element.setCustomValidity()方法得到的文本,可以使用以下

Solution

To retrieve the text which results out from the element.setCustomValidity() method, you can use either of the following Locator Strategies:

  • 使用 Python Mozilla CssSelector :

  • 代码块:

  • Code Block:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://accounts.us1.advisor.ws/user/login")
email_username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.cell.small-21.form-text.required#edit-name[name='name']")))
print(email_username.get_attribute("validationMessage"))

  • 控制台输出:

  • Console Output:

    Please fill out this field.
    

  • 使用 Java Chrome Xpath :

    • 代码块:

    • Code Block:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class validationmessage {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            options.addArguments("--disable-extensions"); 
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://accounts.us1.advisor.ws/user/login");
            WebElement email_username = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='cell small-21 form-text required' and @id='edit-name'][@name='name']")));
            System.out.println(email_username.getAttribute("validationMessage"));
        }
    }
    

  • 控制台输出:

  • Console Output:

    Please fill out this field.
    

  • 这篇关于如何使用Selenium处理html5约束验证弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆