0

I want to Get the email id entered "arjun_123@gmail.com" using java code. I tried the below codes, but it always returned null.

What I really need is, I want to check whether email is successfully entered or not. Sometimes, email is not getting entered. So I want to check whether field is empty or not and retrieve the text entered.

email.getAttribute("innerText");
email.getAttribute("innerHTML");
email.getAttribute("textContent");
email.getText();

code snippet

TylerH
  • 20,799
  • 66
  • 75
  • 101
Arjun
  • 321
  • 3
  • 9
  • 20

3 Answers3

0

Pass your email xpath in this method. You will get entered text

    public String getTextofInputBox(By by) {

    JavascriptExecutor js = (JavascriptExecutor) driver; 
    String value = (String)((js).executeScript("return arguments[0].value",driver.findElement(by)));
    return value;
}
SeleniumNT
  • 60
  • 7
  • This is massively overcomplicated for little value, selenium already provides a way to get data from WebElement attributes. – Ardesco Jun 20 '19 at 08:38
  • 1
    I have faced same scenario, Even value field will not return the entered tax. It will be done by JavascriptExecutor – SeleniumNT Jun 20 '19 at 08:42
  • The above JavaScript Snippet is returning the value attribute. This cannot work unless the value attribute has been updated in the DOM which means it does exactly the same thing as the built in selenium functionality. It's just additional complexity for absolutely no value. – Ardesco Jun 20 '19 at 09:07
  • i had same scenario. email.getAttribute("value"); this code doesn't work in case when there is nothing in Value attribute. – SeleniumNT Jun 20 '19 at 10:21
  • @NayanThakor Aren't you doing the same as in **`arguments[0].value`** in a much more complicated way? – undetected Selenium Jun 20 '19 at 10:26
  • Unfortunately, arguments[0].value gives me result every time. – SeleniumNT Jun 20 '19 at 10:29
0

You are pulling back the wrong attribute, the one you want to use is value:

email.getAttribute("value");
Ardesco
  • 7,281
  • 26
  • 49
  • value attribute returns the value only if value attribute is specified in the html DOM. It may not work sometimes. Better we can use javascript executor as in the @Nayan Thakor's answer – Murthi Jun 20 '19 at 08:58
  • If you type some text into an input element it will set the value attribute in the DOM. That's how elements work. The JavaScript solution below is also reading the value attribute from the DOM. It's replicating Selenium functionality. – Ardesco Jun 20 '19 at 09:05
  • It is not case always. it dependent on the developer code. It may not set the value in some application. – Murthi Jun 20 '19 at 09:12
  • It is the case for an `` element, this is how they work: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#value. I would agree that you can't rule out the fact that a developer can purposefully do something totally stupid (just like I can't guarantee a developer wouldn't redefine true as false in JavaScript), but in 99.99% of cases it will work and if it doesn't it's a bug the developer needs to fix. – Ardesco Jun 20 '19 at 09:26
0

<input> element

As per the documentation HTML <input> element is an interactive controls for HTML forms to accept data from the user.


Data validation

Data validation helps us to ensure that user have filled in the desired fields in the correct format to ensure that submitted data will be successfully accepted by the Application/Web Server.

As per the snapshot:

validate

The <input> element contains JavaScript validation through the attribute data-validate which have 2 sub-validations as 'validate-email':true and 'required':true. These Javasript validations validates the text entered within the <input> field.

So once the Javasript validation completes you can extract the text you have entered in the EMAIL field i.e. arjun_123@gmail.com inducing WebDriverWait using the following solution:

System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(email).getAttribute("value"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352