如何识别Selenium中类名包含空格(如:"uniqclass xyz")或存在多个相似类名的对象



请参阅下面的HTML代码

<div class="dgrid-content ui-widget-content" tabindex="0">
<div id="uniqName-36397" class=" dgrid default Item001">
<div id="uniqName-36780" class=" dgrid default Item003">

使用XPath。你可以试试:

"//div[starts-with(@id, 'uniqName-')]"

这将获取其id属性值以uniqName-开头的所有元素。

您可以使用以下代码对每个元素进行迭代:

IList<IWebElement> elements = driver.FindElements(By.XPath("//div[starts-with(@id, 'uniqName-')]"));
foreach(IWebElement element in elements) {
    //Do Something
}

最新更新