量角器使用定位器找不到任何元素:By(xpath,

时间:2017-09-28 08:32:19

标签: xpath

其实我在浏览器中直接测试,我收到的错误是没有找到使用locator找到的元素。

spec.js

 it('Test for Application Registration text', function () { 
     var ar = browser.findElement(by.xpath('/html/body/admin-app-root/layout/div[1]/cheader/nav/div/div[1]/a[2]'));
     expect(ar.getAttribute('value')).toEqual('Application Registration'); 
 });

HTML:

<div _ngcontent-bbu-25="" class="navbar-header"> 
 <a _ngcontent-bbu-25="" class="navbar-brand logo navbar-brand-logo" href="/register/core/feature-list"></a> 
 <a _ngcontent-bbu-25="" class="navbar-brand navbar-brand-title app-title ellipses" href="/register/core/feature-list">Application Registration</a>
</div>

错误:

Message:[chrome #01]     Failed: no such element: Unable to locate element:
{"method":"xpath","selector":"/html/body/adminapproot/layout/div[1]/
cheader/nav/div/div[1]/a[2]"}[chrome #01]       
(Session info: chrome=61.0.3163.100)[chrome #01]      
(Driver info:chromedriver=2.32.498550(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),
 platfor=Windows NT 10.0.14393 x86_64)[chrome #01]   
 Stack:[chrome #01]   NoSuchElementError: no such element: Unable to locate 
 element:{"method":"xpath","selector":"/html/body/admin-app-
 root/layout/div[1]/cheader/nav/div/div[1]/a[2]"}[chrome #01]       
(Session info: chrome=61.0.3163.100)[chrome #01]       
(Driver info:chromedriver=2.32.498550(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),
 platfor=Windows NT 10.0.14393 x86_64)[chrome #01]        
 at WebDriverError(C:\\node_modules\proractor\node_modules\selenium-
 webdriver\lib\error.js:27:5)

1 个答案:

答案 0 :(得分:1)

这适用于此代码:

it('Test for Application Registration text', function () {
        var EC = protractor.ExpectedConditions;
        var ar = element(by.xpath('/html/body/admin-app-root/layout/div[1]/c-header/nav/div/div[1]/a[2]'));
        browser.wait(EC.visibilityOf(ar));
        expect(ar.getAttribute('value')).toEqual('Application Registration');
    });