Not able to find xpath for field validation error message

1,362 views
Skip to first unread message

Gaurang shah

unread,
Jul 9, 2016, 5:13:46 AM7/9/16
to webdriver
Hi Guys, 

I am trying to test one of the field for field validation error message. The problem is I am not able to find the xpath or css of error message. I am not even able to search the string anywhere in the whole html page. 

Can someone help me identify this element. 

Do not provide anything and press enter. 

I want to identify the xpath for field validation error message "Please enter an email address." 

Following won't work. because this text appears in javascript as well and will always be present. 

//*[contains(text(),'Please enter an email address.'] 

PeterJeffreyGale

unread,
Jul 9, 2016, 5:54:47 AM7/9/16
to webdriver
Talk to the developers about how this error message has been implemented.

If you can't talk to the developers, why are you trying to automate it? If you just want practice, check their terms of use perhaps it's best to choose another site.

darrell grainger

unread,
Jul 9, 2016, 10:27:32 AM7/9/16
to webdriver
This is a feature of HTML. The input for the email address has an attribute indicating that it is 'required'. In order to test that this input will give the error message you really just want to see if the input element has a 'required' attribute...

WebElement emailAddress = driver.findElement(By.cssSelector("[name='emailAddress']"));
String required = emailAddress.getAttribute("required");
assertEquals(required, "true");

If this passes then you should see the error message when someone submits the form without anything in the email address field. If this passes and the error message does not appear then the browser is broken.

Gaurang shah

unread,
Jul 9, 2016, 11:53:26 AM7/9/16
to webd...@googlegroups.com
Hi Darrell, 

Thanks for the information. do you know any online documentation which explains how this work, I realized that it's browser specific, I get a different error message for a wrong email on chrome but not on firefox. 

I tried to verify if text is present on the web page or not, however it's not working. Following is the function I am using to verify. 

Is there any way I could verify if the text is present on the webpage or not?
public boolean isTextPresent(String text){
try{
driver.findElement(By.xpath("//*[contains(.,'"+text+"')]"));
return true;
}catch (NoSuchElementException ne){
return false;
}

}

Gaurang Shah
Blog: AutomationTesting.co.in
Mobile: +91 7387756556

--
You received this message because you are subscribed to a topic in the Google Groups "webdriver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webdriver/FR8adSb5UsE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell grainger

unread,
Jul 10, 2016, 8:38:57 AM7/10/16
to webdriver
The required attribute is describe as part of HTML5 (https://www.w3.org/TR/html5/forms.html#the-required-attribute). If the browser supports HTML5 and the HTML document is HTML5 then having the required field is defined by the HTML5 standard. There is nothing in the HTML5 standard specifying what the error message is. So each browser can implement it differently. What the error message is and how the browser handles it is controlled by the browser.

The error message does not need to exist in the DOM. It is being displayed by the browser. The browser could add it to the DOM (in which case Selenium could see it) or it could just display it as part of the browser application and not visible in the DOM at all.

As for why you get a different error message when the email is supplied but invalid this is because of the pattern attribute (https://www.w3.org/TR/html5/forms.html#the-pattern-attribute). Your web page has the pattern attribute set to the regex value

"^[^@ ]+@[^@ ]+\.[^@ ]+$"

This is a good regex for email addresses but it might be too weak for some email providers and too strong for the RFC standard. The odds you are going to have someone with an email address like "1weird\ email\ @ddress"@foo.com or worse AND they are a valid customer of your website might be so small that they just aren't work supporting.

Dmitri Tikhanski

unread,
Jul 13, 2016, 6:50:50 AM7/13/16
to webdriver
Disclaimer: I'm not very experienced with Selenium, my primary focus is performance testing and I sporadically use Selenium for real user experience measurements (it assumes very basic use cases)

However looking into the https://www.appdirect.com/signup?892353618 page source:













Especially this bit:

window.I18N['user.signup'] = 'Please enter an email address to begin';

It looks like it makes more sense to use JavaScript rather than XPath via JavaScriptExecutor

For instance this is how it looks with Apache JMeter and WebDriver Sampler

WDS.sampleResult.sampleStart()
var signupText = WDS.browser.executeScript('return I18N[\'user.signup\']')
WDS.log.info('Text: ' + signupText)
WDS.sampleResult.sampleEnd()

Demo:


 












I'm not telepathic enough to guess what Selenium client language you're using, above example assumes JavaScript. Here is how it looks being translated to Java:

driver.get("https://www.appdirect.com/signup?1452147832");
String signupText = driver.executeScript("return I18N['user.signup']").toString();
System.out.println(signupText);

Hope this helps. 

Harshad Sane

unread,
Jul 14, 2016, 8:13:13 AM7/14/16
to webd...@googlegroups.com

Hi,
  Can you please try to use the getAttribute("validationmessage") method on the email id field in which you can pass the attribute which you can see in the Firebug on the right hand side in DOM elements.

Regards,
Harshad

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages