How can I get the locator value from a web element?

I would like to get the value of the selector used from the selenium web element in javascript.

If I have an object: var el = browser.driver.findElement(by.id('testEl'));

I would like to get this text testEl and use it elsewhere. I don't care what type of selector (by id, css, etc.), Exactly what the text is.

In the transporter, I could do this: console.log(el.locator().value); This will return the text "testEl".

But with selectors not related to selenium selenium, I was told that .value () is not a function.

Is there any way to infer this locator / selector value?

EDIT:

Goal: capture locator text from selenium web element

Usage: for functions like getVisibleElement

Situation: working with a page where there may be an unknown number of elements of a certain selector, some are hidden and some are not (but the area above them is hidden, there is no hidden tag on this specific part of the element to work with), and I would like to get only visible ones.

In the conveyor:

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 would like to get a locator so that if I ever change what the selector has, I only need to change it in one place - the object of the page where the element is declared.

I can use var to store a string and pass it at any time when I declare an element or try to use something like the above, but that just means using the new standard for setting page objects. It would be very convenient to get access to a locator in selenium, for example, a protractor.

+4
1

webElement, , . , .

webdriver.js :

*     var link = element.findElement(firstVisibleLink);
*
*     function firstVisibleLink(element) {
*       var links = element.findElements(By.tagName('a'));
*       return webdriver.promise.filter(links, function(link) {
*         return links.isDisplayed();
*       }).then(function(visibleLinks) {
*         return visibleLinks[0];
*       });
*     }

, :

var firstElement = function(locator) {
    return browser.driver.findElement(locator);
};
var firstVisible = function(locator) {
    var elements = browser.driver.findElements(locator);
    return browser.driver.promise.filter(elements, function(el) {
        return el.isDisplayed();
    }).then(function(visibleElements) {
        return visibleElements[0];
    });
};

var testLocator = by.css('[name="test"]');
var E1 = firstElement(testLocator);
var E1v = firstVisible(testLocator);
0

Source: https://habr.com/ru/post/1606997/


All Articles