Xpath无效:TypeError:无法将表达式转换为返回指定的类型

时间:2018-10-03 06:54:53

标签: selenium selenium-webdriver robotframework

我正在使用Robot Framework使用硒

xpath is invalid: TypeError: The expression cannot be converted to return the specified type.

我使用的代码是

Set Test Variable                                 ${xpathIP}              xpath=//ul/li/div[@class="segmentName"]
${IPSegmentsCnt}=    Get Matching Xpath Count     ${xpathIP}
Log     ${IPSegmentsCnt}
:For        ${i}    IN RANGE    1    ${IPSegmentsCnt} + 1
 \    ${name}=      Get Text         xpath=(${xpathIP})[${i}]
 \     Log          ${name}
 \    Should Not Match Regexp        ${name}            \\(DS:.+\\)

我没有得到确切的错误信息...

HTML:

<li _ngcontent-ats-90="">
    <span _ngcontent-ats-90="" ng-reflect-class-name="arrow collapse-false" class="arrow collapse-false"></span>
    
<md-checkbox _ngcontent-ats-90="" class="mat-accent mat-checkbox ng-untouched ng-pristine ng-valid" ng-reflect-name="4INFO IP Segments"><label class="mat-checkbox-layout"><div class="mat-checkbox-inner-container"><input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" ng-reflect-id="input-md-checkbox-1" id="input-md-checkbox-1" ng-reflect-name="4INFO IP Segments" name="4INFO IP Segments" tabindex="0" aria-label="">
    <div class="mat-checkbox-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLElement]" ng-reflect-centered="true" ng-reflect-speed-factor="0.3"></div><div class="mat-checkbox-frame"></div><div class="mat-checkbox-background"><svg xml:space="preserve" class="mat-checkbox-checkmark" version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path></svg><div class="mat-checkbox-mixedmark"></div></div></div><span class="mat-checkbox-label">
    </span></label></md-checkbox>
    <div _ngcontent-ats-90="" class="datasourceName">4INFO IP Segments  false</div>
    <ul _ngcontent-ats-90="">
        <li _ngcontent-ats-90="">
            <md-checkbox _ngcontent-ats-90="" class="mat-accent mat-checkbox ng-untouched ng-pristine ng-valid" ng-reflect-name="4Info_Age_18-30"><label class="mat-checkbox-layout"><div class="mat-checkbox-inner-container"><input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" ng-reflect-id="input-md-checkbox-2" id="input-md-checkbox-2" ng-reflect-name="4Info_Age_18-30" name="4Info_Age_18-30" tabindex="0" aria-label=""><div class="mat-checkbox-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLElement]" ng-reflect-centered="true" ng-reflect-speed-factor="0.3"></div><div class="mat-checkbox-frame"></div><div class="mat-checkbox-background"><svg xml:space="preserve" class="mat-checkbox-checkmark" version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path></svg><div class="mat-checkbox-mixedmark"></div></div></div><span class="mat-checkbox-label">
            </span></label></md-checkbox>
            <div _ngcontent-ats-90="" class="segmentName">4Info_Age_18-30 (DS: 0) (CPM: $1.00)</div>
        </li><li _ngcontent-ats-90="">
           <md-checkbox _ngcontent-ats-90="" class="mat-accent mat-checkbox ng-untouched ng-pristine ng-valid" ng-reflect-name="4Info_Age_35_and_over"><label class="mat-checkbox-layout"><div class="mat-checkbox-inner-container"><input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" ng-reflect-id="input-md-checkbox-3" id="input-md-checkbox-3" ng-reflect-name="4Info_Age_35_and_over" name="4Info_Age_35_and_over" tabindex="0" aria-label=""><div class="mat-checkbox-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLElement]" ng-reflect-centered="true" ng-reflect-speed-factor="0.3"></div><div class="mat-checkbox-frame"></div><div class="mat-checkbox-background"><svg xml:space="preserve" class="mat-checkbox-checkmark" version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path></svg><div class="mat-checkbox-mixedmark"></div></div></div><span class="mat-checkbox-label">
            </span></label></md-checkbox>
            <div _ngcontent-ats-90="" class="segmentName">4Info_Age_35_and_over (DS: 0) (CPM: $1.00)</div>
        </li><li _ngcontent-ats-90="">
            <md-checkbox _ngcontent-ats-90="" class="mat-accent mat-checkbox ng-untouched ng-pristine ng-valid" ng-reflect-name="4Info_Age_50_plus"><label class="mat-checkbox-layout"><div class="mat-checkbox-inner-container"><input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" ng-reflect-id="input-md-checkbox-4" id="input-md-checkbox-4" ng-reflect-name="4Info_Age_50_plus" name="4Info_Age_50_plus" tabindex="0" aria-label=""><div class="mat-checkbox-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLElement]" ng-reflect-centered="true" ng-reflect-speed-factor="0.3"></div><div class="mat-checkbox-frame"></div><div class="mat-checkbox-background"><svg xml:space="preserve" class="mat-checkbox-checkmark" version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path></svg><div class="mat-checkbox-mixedmark"></div></div></div><span class="mat-checkbox-label">
            </span></label></md-checkbox>
            <div _ngcontent-ats-90="" class="segmentName">4Info_Age_50_plus (DS: 0) (CPM: $1.00)</div>
        </li><li _ngcontent-ats-90="">
            
    </ul>
</li>

我尝试了多次却没用

1 个答案:

答案 0 :(得分:2)

问题可能是您为xpath分配了两次,因此${name}寻找类似xpath=xpath='yourxpath'的东西

更改

${IPSegmentsCnt}=    Get Matching Xpath Count     ${xpathIP}
Set Test Variable  ${xpathIP}   xpath=//ul/li/div[@class="segmentName"]

Set Test Variable   ${xpathIP}   //ul/li/div[@class="segmentName"]
${IPSegmentsCnt}=    Get Matching Xpath Count     xpath=${xpathIP}