无法从”class”属性为 “list-item-value ng-binding” 的元素中获取文本 – Selenium Webdriver

huangapple go评论70阅读模式
英文:

Unable to get attribute text from "list-item-value ng-binding" class - Selenium Webdriver

问题

我有一个 Selenium 脚本,我试图从以下位置获取 "list-item-value ng-binding" 类的元素,这是最后一次手动运行的信息:

<div class="krn-modal-window">
    <!-- ngIf: popupContentOptions.headerCancelButton.showButton --><button ng-disabled="popupContentOptions.headerCancelButton.action.disabled" class="jqx-window-close-button jqx-window-close-button-bluefin jqx-icon-close jqx-icon-close-bluefin ng-scope" ng-click="handleAction(popupContentOptions.headerCancelButton.action.clickHandlerName, popupContentOptions.headerCancelButton.action.actionName);" ng-if="popupContentOptions.headerCancelButton.showButton" autofocus=""></button>
    <!-- end ngIf: popupContentOptions.headerCancelButton.showButton -->
    <!-- ngIf: popupContentOptions.showHeader -->
    <div class="modal-window__header ng-scope" ng-if="popupContentOptions.showHeader">
        <h4 ng-show="popupContentOptions.title.text" ng-bind="popupContentOptions.title.text | i18n" title="[undefined]" class="ng-binding ng-hide">[undefined]</h4>
        <h4 ng-show="popupContentOptions.title.localizedText" ng-bind="popupContentOptions.title.localizedText" title="Shift Builder" class="ng-binding">Shift Builder</h4>
    </div>
    <!-- end ngIf: popupContentOptions.showHeader -->
    <div class="modal-window__body ng-scope">
        <ul class="list-group">
            <li><b ng-bind="::'html.eventManager.properties.jobId'| i18n" class="list-item-label ng-binding">Job ID</b>:
                <div ng-bind="::modal.properties.jobId" class="list-item-value ng-binding">209</div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.lastRun'| i18n" class="list-item-label ng-binding">Last run</b>:
                <div ng-bind="::modal.properties.lastRun" class="list-item-value ng-binding"></div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.nextRun'| i18n" class="list-item-label ng-binding">Next run</b>:
                <div ng-bind="::modal.properties.nextRun" class="list-item-value ng-binding">25/06/2023 1:27</div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.modifiedBy'| i18n" class="list-item-label ng-binding">Modified By</b>:
                <div ng-bind="::modal.properties.modifiedBy" class="list-item-value ng-binding">Updater for 209</div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.modifiedDate'| i18n" class="list-item-label ng-binding">Modified Date</b>:
                <div ng-bind="::modal.properties.modifiedDate" class="list-item-value ng-binding">15/03/2010 12:35</div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.lastManualRun'| i18n" class="list-item-label ng-binding">Last Manual Run</b>:
                <div ng-bind="::modal.properties.lastManualRun" class="list-item-value ng-binding">19/06/2023 11:45</div>
            </li>
            <li><b ng-bind="::'html.eventManager.properties.errorText'| i18n" class="list-item-label ng-binding">Error text</b>:
                <div ng-bind="::modal.properties.errorText" class="list-item-value ng-binding"></div>
            </li>
        </ul>
    </div>
</div>

我尝试使用列表并循环遍历以列出值,但似乎无法找到 <li> 标签内的元素。对于这种类型的表格,有什么建议吗?

英文:

I have a Selenium script where I am trying to get the "list-item-value ng-binding" class from the Last Manual Run from the following:

  &lt;div class=&quot;krn-modal-window&quot;&gt;
&lt;!-- ngIf: popupContentOptions.headerCancelButton.showButton --&gt;&lt;button ng-disabled=&quot;popupContentOptions.headerCancelButton.action.disabled&quot; class=&quot;jqx-window-close-button jqx-window-close-button-bluefin jqx-icon-close jqx-icon-close-bluefin ng-scope&quot; ng-click=&quot;handleAction(popupContentOptions.headerCancelButton.action.clickHandlerName, popupContentOptions.headerCancelButton.action.actionName);&quot; ng-if=&quot;popupContentOptions.headerCancelButton.showButton&quot; autofocus=&quot;&quot;&gt;&lt;/button&gt;
&lt;!-- end ngIf: popupContentOptions.headerCancelButton.showButton --&gt;
&lt;!-- ngIf: popupContentOptions.showHeader --&gt;
&lt;div class=&quot;modal-window__header ng-scope&quot; ng-if=&quot;popupContentOptions.showHeader&quot;&gt;
&lt;h4 ng-show=&quot;popupContentOptions.title.text&quot; ng-bind=&quot;popupContentOptions.title.text | i18n&quot; title=&quot;[undefined]&quot; class=&quot;ng-binding ng-hide&quot;&gt;[undefined]&lt;/h4&gt;
&lt;h4 ng-show=&quot;popupContentOptions.title.localizedText&quot; ng-bind=&quot;popupContentOptions.title.localizedText&quot; title=&quot;Shift Builder&quot; class=&quot;ng-binding&quot;&gt;Shift Builder&lt;/h4&gt;
&lt;/div&gt;
&lt;!-- end ngIf: popupContentOptions.showHeader --&gt;
&lt;div class=&quot;modal-window__body ng-scope&quot;&gt;
&lt;ul class=&quot;list-group&quot;&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.jobId&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Job ID&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.jobId&quot; class=&quot;list-item-value ng-binding&quot;&gt;209&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.lastRun&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Last run&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.lastRun&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.nextRun&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Next run&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.nextRun&quot; class=&quot;list-item-value ng-binding&quot;&gt;25/06/2023 1:27&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.modifiedBy&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Modified By&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.modifiedBy&quot; class=&quot;list-item-value ng-binding&quot;&gt;Updater for 209&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.modifiedDate&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Modified Date&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.modifiedDate&quot; class=&quot;list-item-value ng-binding&quot;&gt;15/03/2010 12:35&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.lastManualRun&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Last Manual Run&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.lastManualRun&quot; class=&quot;list-item-value ng-binding&quot;&gt;19/06/2023 11:45&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.errorText&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Error text&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.errorText&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

</div>

I've tried to use the list and loop through to list the values but it doesn't seem to like finding the elements within the <li> tags.

Any ideas as I've not really got much experience dealing with these types of tables?

答案1

得分: 0

尝试以下XPath表达式:

//b[text()='Last Manual Run']//following::div[1]

示例代码:

String text = driver.findElement(By.xpath("//b[text()='Last Manual Run']//following::div[1]")).getText();
System.out.println(text);

输出:

19/06/2023 11:45
英文:

Try the below XPath expression:

//b[text()=&#39;Last Manual Run&#39;]//following::div[1]

Example code:

String text = driver.findElement(By.xpath(&quot;//b[text()=&#39;Last Manual Run&#39;]//following::div[1]&quot;)).getText();
System.out.println(text);

Output:

19/06/2023 11:45

答案2

得分: 0

以下是翻译的部分:

给定的HTML:

<div class="modal-window__body ng-scope">
    <ul class="list-group">
        ...
        ...
        ...
        <li><b ng-bind="::'html.eventManager.properties.modifiedDate'| i18n" class="list-item-label ng-binding">Modified Date</b>:
            <div ng-bind="::modal.properties.modifiedDate" class="list-item-value ng-binding">15/03/2010 12:35</div>
        </li>
        <li><b ng-bind="::'html.eventManager.properties.lastManualRun'| i18n" class="list-item-label ng-binding">Last Manual Run</b>:
            <div ng-bind="::modal.properties.lastManualRun" class="list-item-value ng-binding">19/06/2023 11:45</div>
        </li>
        <li><b ng-bind="::'html.eventManager.properties.errorText'| i18n" class="list-item-label ng-binding">Error text</b>:
            <div ng-bind="::modal.properties.errorText" class="list-item-value ng-binding"></div>
        </li>
    </ul>
</div>

所需的<div>标签是包含文本"Last Manual Run"的<b>标签的兄弟元素,它是一个Angular元素。

解决方案:

要提取文本"19/06/2023 11:45",最好使用WebDriverWait来等待visibilityOfElementLocated(),并且可以使用以下任一定位策略

  • 使用_xpath_和_following_:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//b[contains(., 'Last Manual Run')]//following::div[1]"))).getText());
    
  • 使用_xpath_和_following-sibling_:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//b[contains(., 'Last Manual Run')]//following-sibling::div[1]"))).getAttribute("innerHTML"));
    
英文:

Given the HTML:

&lt;div class=&quot;modal-window__body ng-scope&quot;&gt;
&lt;ul class=&quot;list-group&quot;&gt;
...
...
...
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.modifiedDate&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Modified Date&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.modifiedDate&quot; class=&quot;list-item-value ng-binding&quot;&gt;15/03/2010 12:35&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.lastManualRun&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Last Manual Run&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.lastManualRun&quot; class=&quot;list-item-value ng-binding&quot;&gt;19/06/2023 11:45&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b ng-bind=&quot;::&#39;html.eventManager.properties.errorText&#39;| i18n&quot; class=&quot;list-item-label ng-binding&quot;&gt;Error text&lt;/b&gt;:
&lt;div ng-bind=&quot;::modal.properties.errorText&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

The desired &lt;div&gt; tag is the sibling of the &lt;b&gt; tag containing the text Last Manual Run and is an Angular element.


Solution

To extract the text 19/06/2023 11:45 ideally you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following locator strategies:

  • Using xpath and following:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//b[contains(., &#39;Last Manual Run&#39;)]//following::div[1]&quot;))).getText());
    
  • Using xpath and following-sibling:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//b[contains(., &#39;Last Manual Run&#39;)]//following-sibling::div[1]&quot;))).getAttribute(&quot;innerHTML&quot;));
    

huangapple
  • 本文由 发表于 2023年6月19日 23:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定