SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.66k stars 8.19k forks source link

Get the locator from IWebElement #8307

Closed onkarjit closed 4 years ago

onkarjit commented 4 years ago

I'm doing the following:

var locators = new ByChained(By.XPath("//div[@id='historyModal']//div[@class='modal-body']", By.ClassName("historyItem"));
var historyValues = WebDriver.FindElements(locators);

foreach (var historyValue in historyValues)
{
      if (!TrySelectHistory(historyValue))
      {
           // do something
      }
}

private bool TrySelectHistory(IWebElement historyValue)
{
    var historyValueChildren = historyValue.FindElements(By.XPath("./*"));

    if (historyValueChildren.Count < 2) return false;

    var selectButton = historyValueChildren[1].FindElement(By.TagName("button")); // would be great if I could get the locator of this element

    TestHelper.WaitForLoading(element: selectButton); // so I would pass the locator here instead of the element

    TestHelper.JavaScriptClick(element: selectButton); // so I would pass the locator here instead of the element

    return true;
}

As can be seen in the code, the selectButton I get from nested sequence of previous By. I would like to get the locator for this element, in order to prevent StaleElementReferenceException. Because in Selenium once a WebElement goes stale, it always stays stale, however, a locator could be used to keep trying to find the element until it gets a non-stale element.

ghost commented 4 years ago

๐Ÿ‘‹ Hi there! Thank you for creating this issue.

I am the Selenium Assistant Bot ๐Ÿค–, I triage issues in this repository. If I can't do it, I label it to help maintainers identify issues that need triaging.

I am an Open Source project ๐Ÿ™Œ, post bugs or ideas here!

ghost commented 4 years ago

โ—๏ธ It seems this issue is not using any of the supported templates

๐Ÿ’ก Supported issue types are (they start with):

Issue templates help this project to stay in shape, please use them and fill them out completely. By doing that you are helping the project because the community and maintainers can provide prompt feedback, and potentially solve the issue.

If you are asking a question, a better way to address this is:

If you think this is incorrect, please feel free to open a new issue.

Thank you for your contributions.