如何使用Selenium和Java在https://www.phptravels.net/网站中提取HTML5约束验证的文本? [英] How can I extract the text of HTML5 Constraint validation in https://www.phptravels.net/ website using Selenium and Java?

查看:87
本文介绍了如何使用Selenium和Java在https://www.phptravels.net/网站中提取HTML5约束验证的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试切换到警报,但未显示任何此类警报发现错误. 而且我也尝试过ifranes,窗口处理. 弹出窗口仅停留1-2秒,我无法使用inspect元素获取该窗口的xpath. 请检查附件中的scrrenshot.

I have tried switch to alert but it's showing no such alert found error. And i have also tried ifranes,windowhandling. The popup stays for only 1-2 sec and I can't use inspect element to get the xpath of that. Please check the scrrenshot attached.

推荐答案

您所引用的https://www.phptravels.net/中的警报窗口

The alert window in https://www.phptravels.net/ 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 the following solution:

  • 代码块:

  • Code Block:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;

public class HTML5_input_field_validation_message {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.phptravels.net/");
        WebElement checkin = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form.input-lg.dpd1[name='checkin']")));
        System.out.println(checkin.getAttribute("validationMessage"));
    }
}

  • 控制台输出:

  • Console Output:

    Please fill out this field.
    

  • 这篇关于如何使用Selenium和Java在https://www.phptravels.net/网站中提取HTML5约束验证的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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