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

Support FindElements (Multiple) from ShadowRoot #235

Closed
mialeska opened this issue May 5, 2023 · 1 comment · Fixed by #236
Closed

Support FindElements (Multiple) from ShadowRoot #235

mialeska opened this issue May 5, 2023 · 1 comment · Fixed by #236
Assignees
Labels
enhancement New feature or request

Comments

@mialeska
Copy link
Contributor

mialeska commented May 5, 2023

Instead of XPath generation, we can try to generate css selector:

  function generateSelector(element) {
     let selectors = [];
     while (element) {
        let selector = '';
        if (element.id) {
           selector = '#' + element.id;
        } else {
           selector = element.tagName;
        }
        selectors.unshift(selector);
        element = element.parentNode;
     }
     return selectors.join(' > ');
  }

https://www.tutorialspoint.com/how-to-create-a-function-generateselector-to-generate-css-selector-path-of-a-dom-element

or

        const generateSelector = (target) => {
        const selectorPath = [];

        while (target.tagName) {
            let i = 0;

            if (target.parentNode) {
                const children = target.parentNode.children;

                while (i < children.length && children[i] !== target) {
                    i++;
                }
            }

            selectorPath.unshift(target.nodeName + (
                i > 0 ? `:nth-child(${i + 1})` : ''));
            target = target.parentNode;
        }

        return selectorPath.join(' > ');
    }

https://www.geeksforgeeks.org/how-to-create-a-function-generateselector-to-generate-css-selector-path-of-a-dom-element/

or
https://dev.to/aniket_chauhan/generate-a-css-selector-path-of-a-dom-element-4aim
or
https://stackoverflow.com/questions/3620116/get-css-path-from-dom-element

@mialeska
Copy link
Contributor Author

mialeska commented May 8, 2023

Decided to use the most compatible script (based on this answer https://stackoverflow.com/a/28997302):

function previousElementSibling (element) {
  if (element.previousElementSibling !== 'undefined') {
    return element.previousElementSibling;
  } else {
    // Loop through ignoring anything not an element
    while (element = element.previousSibling) {
      if (element.nodeType === 1) {
        return element;
      }
    }
  }
}
function getPath (element) {
  // False on non-elements
  if (!(element instanceof HTMLElement)) { return false; }
  var path = [];
  while (element.nodeType === Node.ELEMENT_NODE) {
    var selector = element.nodeName;
    if (element.id) { selector += ('#' + element.id); }
    else {
      // Walk backwards until there is no previous sibling
      var sibling = element;
      // Will hold nodeName to join for adjacent selection
      var siblingSelectors = [];
      while (sibling !== null && sibling.nodeType === Node.ELEMENT_NODE) {
        siblingSelectors.unshift(sibling.nodeName);
        sibling = previousElementSibling(sibling);
      }
      // :first-child does not apply to HTML
      if (siblingSelectors[0] !== 'HTML') {
        siblingSelectors[0] = siblingSelectors[0] + ':first-child';
      }
      selector = siblingSelectors.join(' + ');
    }
    path.unshift(selector);
    element = element.parentNode;
  }
  return path.join(' > ');
}

@mialeska mialeska self-assigned this May 8, 2023
@mialeska mialeska added this to To do in Aquality Selenium via automation May 8, 2023
mialeska added a commit to aquality-automation/aquality-selenium-java that referenced this issue May 9, 2023
- add JavaScript to generate CSS selector from element
- try to generate CSS selector if XPath generation fails - necessary for ShadowRoot elements since XPath doesn't work for them

Related to aquality-automation/aquality-selenium-dotnet#235
@mialeska mialeska moved this from To do to Review in Progress in Aquality Selenium May 15, 2023
Aquality Selenium automation moved this from Review in Progress to Done Jan 25, 2024
mialeska added a commit to aquality-automation/aquality-selenium-java that referenced this issue Jan 25, 2024
* Support Finding of Multiple elements from ShadowRoot
- add JavaScript to generate CSS selector from element
- try to generate CSS selector if XPath generation fails - necessary for ShadowRoot elements since XPath doesn't work for them

Related to aquality-automation/aquality-selenium-dotnet#235

* Update ElementFactory to use generate CSS locator logic in generateLocator method instead of generateXPathLocator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

1 participant