机器人框架:如何从div标签创建列表

时间:2018-10-30 15:39:23

标签: html list robotframework

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

<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}

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

能否请您帮我执行此操作?

谢谢

1 个答案:

答案 0 :(得分:1)

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

我正在运行Python 3.7.0和http.server,以使用命令7800在端口python -m http.server 7800上提供以下 example.html 来启动监听的Web服务器。端口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
==============================================================================