Amrou
Amrou

Reputation: 489

element.getAttribute("value") returning null from input element

I'm testing a UI using protractor

I'm doing the following

const input_field = element(by.name(name_here));
input_field.getAttribute('value').then(val=>{
    console.log(val
});

And this always prints null, which is weird because when I do it in the browser's console like this:

document.getElementsByName(name_here)[0].value it prints the correct value.

Upvotes: 0

Views: 941

Answers (3)

DMur
DMur

Reputation: 659

If you're using C# this works, once you have your fields defined:

IWebElement inputField= driver.FindElement(By.Id("inputFieldId"));
IWebElement headingLabel= driver.FindElement(By.Id("headingLabelId"));

For <p> tags etc.:

string HeadingValue = headingLabel.Text;

And this works for input fields:

string FieldValue = inputField.GetAttribute("value");

You can combine them to check against each other if necessary like this:

Assert.Equal(inputField.GetAttribute("value"), headingLabel.Text);

Or NotEqual:

Assert.NotEqual(inputField.GetAttribute("value"), headingLabel.Text);

ChromeDriver v.96.0.4664.4500

WebDriver v.4.0.1

Might help someone who ends up here from a Google search at some point.

Upvotes: 0

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

Temp solution

I found a workaround that just worked with chrome 91.0.4472.114 on mac

The problem described here is actually a bug in chromedriver https://bugs.chromium.org/p/chromium/issues/detail?id=1205107 so it is not Protractor problem only

While it's being worked on, you can use chromedriver version 90. Works like a charm. The latest version available is here https://chromedriver.storage.googleapis.com/LATEST_RELEASE_90.0.4430

You can downgrade chromedriver by running this command

webdriver-manager update --gecko=false --versions.chrome 90.0.4430.24

Note sometimes you have global, project local and Protractor specific installations of webdriver-manager. The command I gave will install chromedriver globally. In order to install locally you need to find the path to your webdriver-manager and run command like this

node ./node_modules/protractor/node_modules/webdriver-manager update --gecko=false --versions.chrome 90.0.4430.24

And some of you may need to run command in sudo mode. Keep it in mind

Upvotes: 0

Amrou
Amrou

Reputation: 489

As a workaround for now and up until this bug gets fixed, you could use

browser.executeScript('return arguments[0].value', inputElement);

where inputElement is of type ElementFinder

Upvotes: 1

Related Questions