How can I retrieve the selector value used in a selenium webelement in javascript?
Let's say I have the following object:
var el = browser.driver.findElement(by.id('testEl'));
I want to extract the text 'testEl' from this object and utilize it elsewhere, regardless of the selector type (by id, by css, etc.).
In Protractor, I could achieve this using:
console.log(el.locator().value);
This would output the text 'testEl'.
However, when dealing with just plain selenium selectors without Protractor, I am informed that .value() is not a valid function.
Is there any method to retrieve this locator/selector value?
EDIT:
The objective: Obtain the locator text from a selenium webElement
The use case: For functions such as getVisibleElement
The scenario: Working on a page where there may be an unknown number of elements with a specific selector, some hidden and others visible. There is no explicit hidden tag in this section of the element to target. The aim is to only retrieve the visible elements.
In Protractor:
function getVisibleElementByPageObject(protractorWebElement){
var allElementsOfSelector = element.all(by.css(protractorWebElement.locator().value));
var displayedElement = allElementsOfSelector.filter(function(elem) {
return elem.isDisplayed('cannot find' + ' ' + protractorWebElement.locator().value);
}).first();
return displayedElement;
}
var exampleEl = $('[name="test"]');
var visibleExampleEl = getVisibleElementByPageObject(exampleEl);
I wish to access the locator so that if I were to adjust the selector in the future, I would only need to modify it in one location - the page object where the element is defined.
I could store the string in a variable and pass it whenever declaring an element or utilizing similar functions, but it would require creating a new standard for setting up page objects. It would be convenient to access the locator in selenium similarly to how it's done in Protractor above.