英文:
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:
<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>
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()='Last Manual Run']//following::div[1]
Example code:
String text = driver.findElement(By.xpath("//b[text()='Last Manual Run']//following::div[1]")).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:
<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>
The desired <div>
tag is the sibling of the <b>
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("//b[contains(., 'Last Manual Run')]//following::div[1]"))).getText());
-
Using xpath and 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"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论