Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select form elements by associated label #191

Closed
ravinggenius opened this issue Oct 28, 2013 · 7 comments
Closed

Select form elements by associated label #191

ravinggenius opened this issue Oct 28, 2013 · 7 comments

Comments

@ravinggenius
Copy link

Given markup like:

<label for="your_name">Name</label>
<input id="your_name" />

... I should be able to fill in the input with ptor.findElement(protractor.By.label('Name')).sendKeys('My Name');.

Is this currently possible?

@ravinggenius
Copy link
Author

I was able to approximate the desired functionality with

ptor.findElement(protractor.By.xpath('//label[contains(text(), "Name")]')).then(function (label) {
  ptor.findElement(protractor.By.id(label.getAttribute('for'))).sendKeys('My Name');
});

... but clearly this is less than ideal.

@juliemr
Copy link
Member

juliemr commented Oct 31, 2013

I'm confused why you would want to do this. The 'for' attribute in your label has to be the id attribute of your input so... why not just find the input by id?

element(by.id('your_name'));

@ravinggenius
Copy link
Author

Basically I'm asking for a convenience wrapper so Protractor can interact with the page more like a human user would. Using my example above, I believe it is more natural to ask the user (and thus Protractor) to fill in the "Name" field, not the "#your_name" field.

@juliemr
Copy link
Member

juliemr commented Nov 6, 2013

Gotcha. Finding elements by text on the page is often not recommended because labels change more frequently than IDs and internationalization/localization can mess it up. I'd suggest writing your own wrapper if you want to use this.

So, I think a feature like this would be great as an extension but not a core part of Protractor. What I'd like to do is make an easier way to extend the custom element locators that Protractor adds to webdriver.

@juliemr
Copy link
Member

juliemr commented Nov 8, 2013

See the request for that in #236

@juliemr juliemr closed this as completed Nov 8, 2013
@Ema-jar
Copy link

Ema-jar commented Jan 11, 2016

@jamesthomasonjr
Copy link

jamesthomasonjr commented Sep 14, 2018

In case anyone runs across this issue from Google and wants a simple solution using by.addLocator:

function findElementByLabel(labelText, opt_parentElement) {
    const using = opt_parentElement || document;
    const xpath = `//input[@id=string(//label[. = '${labelText}']/@for)]`;
    return document.evaluate(xpath, using, null, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue;
};

by.addLocator('label', findElementByLabel);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants