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

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

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

问题

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

  1. <div class="krn-modal-window">
  2. <!-- 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>
  3. <!-- end ngIf: popupContentOptions.headerCancelButton.showButton -->
  4. <!-- ngIf: popupContentOptions.showHeader -->
  5. <div class="modal-window__header ng-scope" ng-if="popupContentOptions.showHeader">
  6. <h4 ng-show="popupContentOptions.title.text" ng-bind="popupContentOptions.title.text | i18n" title="[undefined]" class="ng-binding ng-hide">[undefined]</h4>
  7. <h4 ng-show="popupContentOptions.title.localizedText" ng-bind="popupContentOptions.title.localizedText" title="Shift Builder" class="ng-binding">Shift Builder</h4>
  8. </div>
  9. <!-- end ngIf: popupContentOptions.showHeader -->
  10. <div class="modal-window__body ng-scope">
  11. <ul class="list-group">
  12. <li><b ng-bind="::'html.eventManager.properties.jobId'| i18n" class="list-item-label ng-binding">Job ID</b>:
  13. <div ng-bind="::modal.properties.jobId" class="list-item-value ng-binding">209</div>
  14. </li>
  15. <li><b ng-bind="::'html.eventManager.properties.lastRun'| i18n" class="list-item-label ng-binding">Last run</b>:
  16. <div ng-bind="::modal.properties.lastRun" class="list-item-value ng-binding"></div>
  17. </li>
  18. <li><b ng-bind="::'html.eventManager.properties.nextRun'| i18n" class="list-item-label ng-binding">Next run</b>:
  19. <div ng-bind="::modal.properties.nextRun" class="list-item-value ng-binding">25/06/2023 1:27</div>
  20. </li>
  21. <li><b ng-bind="::'html.eventManager.properties.modifiedBy'| i18n" class="list-item-label ng-binding">Modified By</b>:
  22. <div ng-bind="::modal.properties.modifiedBy" class="list-item-value ng-binding">Updater for 209</div>
  23. </li>
  24. <li><b ng-bind="::'html.eventManager.properties.modifiedDate'| i18n" class="list-item-label ng-binding">Modified Date</b>:
  25. <div ng-bind="::modal.properties.modifiedDate" class="list-item-value ng-binding">15/03/2010 12:35</div>
  26. </li>
  27. <li><b ng-bind="::'html.eventManager.properties.lastManualRun'| i18n" class="list-item-label ng-binding">Last Manual Run</b>:
  28. <div ng-bind="::modal.properties.lastManualRun" class="list-item-value ng-binding">19/06/2023 11:45</div>
  29. </li>
  30. <li><b ng-bind="::'html.eventManager.properties.errorText'| i18n" class="list-item-label ng-binding">Error text</b>:
  31. <div ng-bind="::modal.properties.errorText" class="list-item-value ng-binding"></div>
  32. </li>
  33. </ul>
  34. </div>
  35. </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:

  1. &lt;div class=&quot;krn-modal-window&quot;&gt;
  2. &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;
  3. &lt;!-- end ngIf: popupContentOptions.headerCancelButton.showButton --&gt;
  4. &lt;!-- ngIf: popupContentOptions.showHeader --&gt;
  5. &lt;div class=&quot;modal-window__header ng-scope&quot; ng-if=&quot;popupContentOptions.showHeader&quot;&gt;
  6. &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;
  7. &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;
  8. &lt;/div&gt;
  9. &lt;!-- end ngIf: popupContentOptions.showHeader --&gt;
  10. &lt;div class=&quot;modal-window__body ng-scope&quot;&gt;
  11. &lt;ul class=&quot;list-group&quot;&gt;
  12. &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;:
  13. &lt;div ng-bind=&quot;::modal.properties.jobId&quot; class=&quot;list-item-value ng-binding&quot;&gt;209&lt;/div&gt;
  14. &lt;/li&gt;
  15. &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;:
  16. &lt;div ng-bind=&quot;::modal.properties.lastRun&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
  17. &lt;/li&gt;
  18. &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;:
  19. &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;
  20. &lt;/li&gt;
  21. &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;:
  22. &lt;div ng-bind=&quot;::modal.properties.modifiedBy&quot; class=&quot;list-item-value ng-binding&quot;&gt;Updater for 209&lt;/div&gt;
  23. &lt;/li&gt;
  24. &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;:
  25. &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;
  26. &lt;/li&gt;
  27. &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;:
  28. &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;
  29. &lt;/li&gt;
  30. &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;:
  31. &lt;div ng-bind=&quot;::modal.properties.errorText&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
  32. &lt;/li&gt;
  33. &lt;/ul&gt;
  34. &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表达式:

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

示例代码:

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

输出:

  1. 19/06/2023 11:45
英文:

Try the below XPath expression:

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

Example code:

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

Output:

  1. 19/06/2023 11:45

答案2

得分: 0

以下是翻译的部分:

给定的HTML:

  1. <div class="modal-window__body ng-scope">
  2. <ul class="list-group">
  3. ...
  4. ...
  5. ...
  6. <li><b ng-bind="::'html.eventManager.properties.modifiedDate'| i18n" class="list-item-label ng-binding">Modified Date</b>:
  7. <div ng-bind="::modal.properties.modifiedDate" class="list-item-value ng-binding">15/03/2010 12:35</div>
  8. </li>
  9. <li><b ng-bind="::'html.eventManager.properties.lastManualRun'| i18n" class="list-item-label ng-binding">Last Manual Run</b>:
  10. <div ng-bind="::modal.properties.lastManualRun" class="list-item-value ng-binding">19/06/2023 11:45</div>
  11. </li>
  12. <li><b ng-bind="::'html.eventManager.properties.errorText'| i18n" class="list-item-label ng-binding">Error text</b>:
  13. <div ng-bind="::modal.properties.errorText" class="list-item-value ng-binding"></div>
  14. </li>
  15. </ul>
  16. </div>

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

解决方案:

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

  • 使用_xpath_和_following_:

    1. 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_:

    1. 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:

  1. &lt;div class=&quot;modal-window__body ng-scope&quot;&gt;
  2. &lt;ul class=&quot;list-group&quot;&gt;
  3. ...
  4. ...
  5. ...
  6. &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;:
  7. &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;
  8. &lt;/li&gt;
  9. &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;:
  10. &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;
  11. &lt;/li&gt;
  12. &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;:
  13. &lt;div ng-bind=&quot;::modal.properties.errorText&quot; class=&quot;list-item-value ng-binding&quot;&gt;&lt;/div&gt;
  14. &lt;/li&gt;
  15. &lt;/ul&gt;
  16. &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:

    1. 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:

    1. 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:

确定