如何使用Java在Selenium中通过文本查找Web元素?

时间:2018-08-29 17:28:08

标签: java selenium selenium-webdriver

我正在尝试查找动态Web元素,因此无法通过ID或Xpath查找。我试图找到使用“ Tab”键或使用其他键盘命令均未成功的方法。

在具有相同条件的另一页上,我尝试了以下代码,并且可以正常工作:

            public void SeachApp() {
            WebElement elem = driver.findElement(By.xpath("//*[contains(text(),'BaseAloca')]"));
                int width = ((WebElement) elem).getSize().getWidth();

                Actions act = new Actions(driver);
                act.moveToElement((WebElement) elem).moveByOffset((width/2)-2 0).click().perform();
        }

但是在这个新页面上不起作用。这是页面:

enter image description here

HTML代码第1页:

<circle class="bubble" r="48" style="filter: url(&quot;https://us.qlikcloud.com/node/i-0fc3fc8cc088b4fb7/sense/app/b5573a15-e951-45b5-81ed-92ef95c916bc/datamanager/datamanager#bubble-drop-shadow&quot;); fill: url(&quot;https://us.qlikcloud.com/node/i-0fc3fc8cc088b4fb7/sense/app/b5573a15-e951-45b5-81ed-92ef95c916bc/datamanager/datamanager#bubble-fill&quot;);"></circle>

<text class="label" y="5" text-anchor="middle">BaseAloca</text>

还有另一页可以单击此页面: enter image description here

HTML代码第2页:

<iframe id="appFrame" ng-mouseenter="refreshCookies()" tenantid="5b5f0e2aa543a3be1cf15913" ng-src="/node/i-01bc83ae6124b5e05/sense/app/22110b43-a3d3-4d3e-8d45-c944f216fbab?instance=qlik-i-01bc83ae6124b5e05" src="/node/i-01bc83ae6124b5e05/sense/app/22110b43-a3d3-4d3e-8d45-c944f216fbab?instance=qlik-i-01bc83ae6124b5e05"></iframe>

<div class="table-inner" ng-class="{ 'selected': isTableSelected(), 'nointeraction':!enableSelection() }" ng-click="selectTable($event)" ng-dblclick="editObject($event)" ng-disabled="true" aria-disabled="true" disabled="disabled">
    <div class="icon lui-icon lui-icon--table" ng-class="{ 'pending-delete': isTableDeleted() }"></div>
    <!-- ngIf: canSplit -->
    <!-- ngIf: showSelectionState  -->
    <div class="table-actions" ng-class="{ 'pending-delete': isTableDeleted() }">
        <!-- ngIf: tableObj.sourceType === 'ScriptTable' -->

        <!-- ngIf: canSplit && !disableButtons && !splitIndividualTablesFeature -->
        <!-- ngIf: canSplit && !disableButtons && splitIndividualTablesFeature -->
        <!-- ngIf: tableObj.isFiltered && !disableButtons && !canSplit -->
        <!-- ngIf: canEdit && !disableButtons --><div class="action-icon lui-icon lui-icon--edit qui-btn-icn lui-btn-div ng-scope" q-title-translation="DataManager.Overview.Tooltip.Edit" ng-if="canEdit &amp;&amp; !disableButtons" ng-click="onTableEditClick($event)" title="Edit this table"></div><!-- end ngIf: canEdit && !disableButtons -->
        <!-- ngIf: canDelete && !disableButtons --><div class="action-icon lui-icon lui-icon--bin qui-btn-icn lui-btn-div ng-scope" q-title-translation="DataManager.Overview.Tooltip.Delete" ng-if="canDelete &amp;&amp; !disableButtons" ng-click="onTableDeleteClick($event)" title="Delete this table"></div><!-- end ngIf: canDelete && !disableButtons -->

    </div>
    <div class="table-information" ng-class="{ 'pending-delete': isTableDeleted() }">
        <div class="table-name ng-binding" ng-bind="tableObj.name" title="BaseAloca">BaseAloca</div>
        <!-- ngIf: tableObj.hint -->
        <div class="table-subtitle ng-binding" ng-bind="tableObj.sourceName" title="Proj.xlsm">Proj.xlsm</div>
    </div>
    <!-- ngIf: !pendingAction --><div class="status-bar ng-scope" ng-class="pendingAction.class" ng-if="!pendingAction">
        <div class="left-side">
            <!-- ngIf: tableObj.fields.length > 0 --><div class="fields ng-binding ng-scope" ng-if="tableObj.fields.length > 0">Fields: 40</div><!-- end ngIf: tableObj.fields.length > 0 -->
            <!-- ngIf: hasGeoField() -->
            <div class="status-icon" ng-class="{true: 'lui-icon lui-icon--script'}[tableObj.sourceType === 'ScriptTable']" q-title-translation="DataManager.Overview.SynchronizeTooltip" title="Click to open the synchronization option for scripted tables"></div>
            <!-- ngIf: tableObj.isFiltered -->
        </div>
    </div><!-- end ngIf: !pendingAction -->
    <!-- ngIf: pendingAction -->

</div>

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用以下代码来识别此特定的“气泡”类元素:

WebElement elem = driver.findElement(By.xpath("//*[name()='circle'][contains(@style,'https://us.qlikcloud.com/node/i-0fc3fc8cc088b4fb7/sense/app/b5573a15-e951-45b5-81ed-92ef95c916bc/datamanager/datamanager#bubble-drop-shadow')]"));

更新#1:

driver.switchTo().frame("appFrame");     //need to switch to this frame before identifying elements present on frame with id "appFrame"
WebElement elem = driver.findElement(By.xpath("//div[@class='table-information']/div[@title='BaseAloca']"));