繁体   English   中英

从 IWebElement 获取定位器

[英]Get the locator from IWebElement

我正在执行以下操作:

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 that locator here instead of the element

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

    return true;
}

从代码中可以看出,我从前一个By的嵌套序列中获得的selectButton 我想获取此元素的定位器,以防止StaleElementReferenceException 因为在 Selenium 中,一旦 WebElement 过时,它总是保持过时,但是,可以使用定位器继续尝试查找元素,直到它得到一个非过时的元素。

您可以将By type 传递给WaitForLoadingJavaScriptClick

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM