【问题标题】:Robot Framework : How to create a list from the div tags机器人框架:如何从 div 标签创建列表
【发布时间】:2019-04-03 17:02:58
【问题描述】:

我想从下面的列表中检索所有文本以创建帐户列表:

<ul class="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu ng-scope" ng-show="$select.open &amp;&amp; $select.items.length > 0" repeat="tenant.id as tenant in tenants | filter:$select.search | orderBy: tenant.id" style="opacity: 1;">
	<li class="ui-select-choices-group" id="ui-select-choices-16">
		<div class="divider ng-hide" ng-show="$select.isGrouped &amp;&amp; $index > 0"></div>
		<div ng-show="$select.isGrouped" class="ui-select-choices-group-label dropdown-header ng-binding ng-hide" ng-bind="$group.name"></div>
		<!-- ngRepeat: tenant in $select.items -->
		<!-- ngIf: $select.open -->
		<div ng-attr-id="ui-select-choices-row-{{ $select.generatedId }}-{{$index}}" class="ui-select-choices-row ng-scope active" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="tenant in $select.items" ng-if="$select.open" ng-click="$select.select(tenant,$select.skipFocusser,$event)" id="ui-select-choices-row-16-0" style="">
			<span class="ui-select-choices-row-inner" uis-transclude-append="">
				<div ng-bind-html="tenant.name | highlight: $select.search" class="ng-binding ng-scope">RF-Account</div>
			</span>
		</div>
		<!-- end ngIf: $select.open -->
		<!-- end ngRepeat: tenant in $select.items -->
		<!-- ngIf: $select.open -->
		<div ng-attr-id="ui-select-choices-row-{{ $select.generatedId }}-{{$index}}" class="ui-select-choices-row ng-scope" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="tenant in $select.items" ng-if="$select.open" ng-click="$select.select(tenant,$select.skipFocusser,$event)" id="ui-select-choices-row-16-1" style="">
			<span class="ui-select-choices-row-inner" uis-transclude-append="">
            	<div ng-bind-html="tenant.name | highlight: $select.search" class="ng-binding ng-scope">RF-Account1</div>
            </span>
        </div>
        <!-- end ngIf: $select.open -->
        <!-- end ngRepeat: tenant in $select.items -->
        <!-- ngIf: $select.open -->
        <div ng-attr-id="ui-select-choices-row-{{ $select.generatedId }}-{{$index}}" class="ui-select-choices-row ng-scope" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="tenant in $select.items" ng-if="$select.open" ng-click="$select.select(tenant,$select.skipFocusser,$event)" id="ui-select-choices-row-16-2">
        	<span class="ui-select-choices-row-inner" uis-transclude-append="">
            	<div ng-bind-html="tenant.name | highlight: $select.search" class="ng-binding ng-scope">RF-Account2</div>
            </span>
        </div>
        <!-- end ngIf: $select.open -->
        <!-- end ngRepeat: tenant in $select.items -->
        <!-- ngIf: $select.open -->
        <div ng-attr-id="ui-select-choices-row-{{ $select.generatedId }}-{{$index}}" class="ui-select-choices-row ng-scope" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}" role="option" ng-repeat="tenant in $select.items" ng-if="$select.open" ng-click="$select.select(tenant,$select.skipFocusser,$event)" id="ui-select-choices-row-16-3">
        	<span class="ui-select-choices-row-inner" uis-transclude-append="">
            	<div ng-bind-html="tenant.name | highlight: $select.search" class="ng-binding ng-scope">RF-Account3</div>
            </span>
        </div>
        <!-- end ngIf: $select.open -->
        <!-- end ngRepeat: tenant in $select.items -->
    </li>
</ul>

这是我的研究:

!Choisir un autre compte
  Click Element    selectTenant
  ${listAccount}    Create List    
  ${idDynamicAccountList}    Get Element Attribute    //li[@class='ui-select-choices-group']    id
  ${rest}    ${group}    Split String From Right    ${idDynamicAccountList}    -    1
  ${maxAccount}    Get Element Count    //li[@class='ui-select-choices-group']//span
  :FOR    ${i}    IN RANGE    0    ${maxAccount}
  \    ${accountXpath}    Catenate    SEPARATOR=    //li[@id='${idDynamicAccountList}']//div[@id='ui-select-choices-row-    ${group}    -    ${i}    ']//span
  \    ${accountName}    Get Text    ${accountXpath}
  \    Append To List    ${listAccount}    ${accountName}
  Log List    ${listAccount}

最后,列表是空的。在我看来,问题一定来自使用关键字“获取文本”,但我不知道为什么。

请您帮我执行此操作好吗?

谢谢

【问题讨论】:

  • 因为Stack Overflow 是我们旨在帮助解决编程问题的地方,这需要您投入一定的时间。这通常是通过提供您的特定技术问题的minimal reproducible example 来实现的。如果您通过阅读 help centerHow to Ask 部分了解什么是好问题,这将有所帮助。
  • 我完全理解。正是因为我一直在寻找前进的方法而做不到。你有一个小的方向可以走吗?
  • 嗯,所以你的结果应该是一个包含 RF-Account,RF-Account1,RF-Account2,RF-Account3 的列表?能否提供您的机器人代码?
  • 我添加了对我的工作 @A 的搜索。 Kootstra, Wojtek

标签: html list robotframework


【解决方案1】:

运行您的示例时,我得到了预期的结果。我唯一更改的是删除 Click Element,因为它会产生问题,我认为这对您来说并不重要。

我正在运行 Python 3.7.0 和 http.server 以在端口 7800 上提供以下 example.html,使用命令 python -m http.server 7800 启动一个监听端口 7800 的网络服务器

<html>
<body>

<ul 
... [snipped the rest of the provided HTML code]
</ul>

</body>
</html>

机器人文件包含以下脚本:

*** Settings ***
Library    SeleniumLibrary    
Library    String    
Library    Collections    

Suite Teardown    Close All Browsers    

*** Test Cases ***
!Choisir un autre compte
  Open Browser    http://localhost:7800/example.html    Chrome

  # Click Element    selectTenant
  ${listAccount}    Create List    
  ${idDynamicAccountList}    Get Element Attribute    //li[@class='ui-select-choices-group']    id
  ${rest}    ${group}    Split String From Right    ${idDynamicAccountList}    -    1
  ${maxAccount}    Get Element Count    //li[@class='ui-select-choices-group']//span
  :FOR    ${i}    IN RANGE    0    ${maxAccount}
  \    ${accountXpath}    Catenate    SEPARATOR=    //li[@id='${idDynamicAccountList}']//div[@id='ui-select-choices-row-    ${group}    -    ${i}    ']//span
  \    ${accountName}    Get Text    ${accountXpath}
  \    Append To List    ${listAccount}    ${accountName}
  Log List    ${listAccount}

请注意,我已删除 Click Element,因为这会产生问题。由于缺少以下日志,并且在 RED Eclipse 插件消息日志中生成输出:

Starting test: Folder.For-Loop.!Choisir un autre compte
20181031 20:42:55.279 : INFO : Opening browser 'Chrome' to base url 'http://localhost:7800/example.html'.
20181031 20:42:58.027 : INFO : ${listAccount} = []
20181031 20:42:58.083 : INFO : ${idDynamicAccountList} = ui-select-choices-16
20181031 20:42:58.085 : INFO : ${rest} = ui-select-choices
20181031 20:42:58.085 : INFO : ${group} = 16
20181031 20:42:58.116 : INFO : ${maxAccount} = 4
20181031 20:42:58.117 : INFO : ${accountXpath} = //li[@id='ui-select-choices-16']//div[@id='ui-select-choices-row-16-0']//span
20181031 20:42:58.198 : INFO : ${accountName} = RF-Account
20181031 20:42:58.199 : INFO : ${accountXpath} = //li[@id='ui-select-choices-16']//div[@id='ui-select-choices-row-16-1']//span
20181031 20:42:58.287 : INFO : ${accountName} = RF-Account1
20181031 20:42:58.293 : INFO : ${accountXpath} = //li[@id='ui-select-choices-16']//div[@id='ui-select-choices-row-16-2']//span
20181031 20:42:58.373 : INFO : ${accountName} = RF-Account2
20181031 20:42:58.377 : INFO : ${accountXpath} = //li[@id='ui-select-choices-16']//div[@id='ui-select-choices-row-16-3']//span
20181031 20:42:58.457 : INFO : ${accountName} = RF-Account3
20181031 20:42:58.463 : INFO : List length is 4 and it contains following items:
0: RF-Account
1: RF-Account1
2: RF-Account2
3: RF-Account3
Ending test: Folder.For-Loop.!Choisir un autre compte

在 RED Eclipse 插件中运行它的控制台:

Command: C:\Users\name\AppData\Local\Programs\Python\Python37\python.exe -m robot.run --listener C:\Users\name\AppData\Local\Temp\RobotTempDir7442861157430042777\TestRunnerAgent.py:60930 --argumentfile C:\Users\name\AppData\Local\Temp\RobotTempDir7442861157430042777\args_ede0037b.arg C:\Users\name\eclipse-workspace\Folder
Suite Executor: Robot Framework 3.0.4 (Python 3.7.0 on win32)
==============================================================================
Folder                                                                        
==============================================================================
Folder.For-Loop                                                               
==============================================================================
!Choisir un autre compte                                              | PASS |
------------------------------------------------------------------------------
Folder.For-Loop                                                       | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Folder                                                                | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================

【讨论】:

  • 现在工作正常。在 ul 标记的类中,ng-hide 参数阻止了帐户列表的可见性,因为它是一个动态类。这就是为什么我的清单是空的。 @kootstra
猜你喜欢
  • 2017-10-26
  • 2020-10-22
  • 2020-01-22
  • 2019-05-09
  • 1970-01-01
  • 2021-11-23
  • 2018-04-01
  • 2016-09-24
  • 2020-10-29
相关资源
最近更新 更多