我正在尝试使用Selenium做一些基本的事情,并且试图通过“文本”找到一个元素,因为我所有的元素都是动态生成的,或者它们具有相同的属性名称和值。
我想在以下div进入“ SelTest”,这是一个块:
<div ng-mouseenter="hover=true" ng-mouseleave="hover=false" class="object workspace" ng-repeat="workspace in workspaces | filter:{'workspace_title':workspaceFilter, 'is_archived':false} | orderBy: 'workspace_title'" ui-sref="main.workspace({workspaceId: workspace.id})" href="#!/workspace/a43d95fa24acbfc4757ba86a4d8dec22">
<i class="mdi mdi-folder"></i>
<div class="title">SelTest</div>
<div class="desc"></div>
<div class="tools ng-hide" ng-show="hover" style="">
<button type="button" ng-click="workspace.toggleArchive();$event.stopPropagation();" class="btn btn-default btn-xs">
<span>Archive</span>
</button>
</div>
</div>
而其余的代码具有相同的确切名称和值。
这是大图:
反正有没有去 SelTest 的地方?
答案 0 :(得分:1)
要找到文本为 SelTest 的元素,您需要为visibilityOfElementLocated()
引入 WebDriverWait ,并且可以使用以下Locator Strategies中的任何一个:
cssSelector
:
WebElement ele = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.object.workspace[ng-repeat^='workspace in workspaces'][href*='workspace'] div.title")));
xpath
:
WebElement ele = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='object workspace' and starts-with(@ng-repeat, 'workspace in workspaces')][contains(@href, 'workspace')]//div[@class='title' and text()='SelTest']")));