NoSuchElementError:没有这样的元素:无法找到量角器的元素

Posted

技术标签:

【中文标题】NoSuchElementError:没有这样的元素:无法找到量角器的元素【英文标题】:NoSuchElementError: no such element: Unable to locate element for protractor 【发布时间】:2017-03-22 01:37:50 【问题描述】:

element(By.xpath(("/html/body/div[2]/div[3]/md-content/md-card/md-card-content/div/div/div/div/md- custom-table/div/md-table-container/table/thead/tr/th[3]/span"))).click();

这是我想让它工作的代码。

这是我想被点击的地方的xpath路径

/html/body/div[2]/div[3]/md-content/md-card/md-card-content/div/div/div/div/md-custom-table/div/md- table-container/table/thead/tr/th[3]/span

这是错误

Message:
  Failed: No element found using locator: By(xpath, /html/body/div[2]/div[3]/md-content/md-card/md-card-content/div/div/div/div/md-custom-table/div/md-table-container/table/thead/tr/th[3]/span) Stack:
  NoSuchElementError: No element found using locator:
  By(xpath, /html/body/div[2]/div[3]/md-content/md-card/md-card-content/div/div/div/div/md-custom-table/div/md-table-container/table/thead/tr/th[3]/span)
    at WebDriverError (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:26:26)
    at NoSuchElementError (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:241:26)
    at /usr/lib/node_modules/protractor/built/element.js:717:27
    at ManagedPromise.invokeCallback_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
    at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
    at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
    at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27
    at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:639:7
    at process._tickCallback (internal/process/next_tick.js:103:7)Error
    at ElementArrayFinder.applyAction_ (/usr/lib/node_modules/protractor/built/element.js:403:27)
    at ElementArrayFinder._this.(anonymous function) [as click] (/usr/lib/node_modules/protractor/built/element.js:101:30)
    at ElementFinder.(anonymous function) [as click] (/usr/lib/node_modules/protractor/built/element.js:740:22)
    at Object.<anonymous> (/home/vegan/hb-productupload/gateway/src/test/javascript/e2e/account/productDashboard/productDashboardControllerSpec.js:36:47)
    at /usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:94:23
    at new ManagedPromise (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1082:7)
    at controlFlowExecute (/usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:80:18)
    at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
    at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
    at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2820:25
  From: Task: Run it("should be able to check product dashboard as a cta") in control flow
    at Object.<anonymous> (/usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:79:14)
    at /usr/lib/node_modules/protractor/node_modules/jasminewd2/index.js:16:5
    at ManagedPromise.invokeCallback_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1379:14)
    at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2913:14)
    at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2896:21)
    at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2775:27
  From asynchronous test: 
  Error
    at Suite.<anonymous> (/home/vegan/hb-productupload/gateway/src/test/javascript/e2e/account/productDashboard/productDashboardControllerSpec.js:33:9)
    at Object.<anonymous> (/home/vegan/hb-productupload/gateway/src/test/javascript/e2e/account/productDashboard/productDashboardControllerSpec.js:8:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)

1 spec, 1 failure Finished in 13.16 seconds

[15:19:25] I/launcher - 0 instance(s) of WebDriver still running
[15:19:25] I/launcher - chrome #01 failed 1 test(s)
[15:19:25] I/launcher - overall: 1 failed spec(s)
[15:19:25] E/launcher - Process exited with error code 1

所以对于同一个地方,这是html:

<span translate="" class="ng-scope ng-binding">Number of Products</span>

这是选择器

body > div.site-content > div.layout-column.flex > md-content > md-card > md-card-content > div > div > div > div > md-custom-table > div > md-table-container > table > thead > tr > th.md-column.ng-scope.ng-isolate-scope.flex.md-sort.md-active > span

对于相同的跨度,这个可以点击

 dashboardPageObject.element.all(by.css('[md-order-by="count"]')).get(1);.click();
            browser.driver.sleep(28);
            dashboardPageObject.element.all(by.css('[md-order-by="count"]')).get(1);.click();

它点击了 2 次,但没有等待 28 秒。

我不在乎 xpath 或 css 的选择器。例如另一个

/html/body/div[2]/div[3]/header/md-content/md-tabs/md-tabs-wrapper/md-tabs-canvas/md-pagination-wrapper/md-tab-item[5]/span

这行得通

 element(by.xpath('/html/body/div[2]/div[3]/header/md-content/md-tabs/md-tabs-wrapper/md-tabs-canvas/md-pagination-wrapper/md-tab-item[5]/span'));

【问题讨论】:

sleep() 时间以毫秒为单位。您正在等待 0.28 秒,而不是 28 秒。 【参考方案1】:

首先,正如我在上面的 cmets 中提到的,sleep() 计时器需要毫秒,而不是秒。所以你只用这个命令等待 0.28 秒。如果你真的想睡 28 秒,可以延长到 28000。

但是,这不是最好的方法。您应该使用 Expected Conditions 形式的隐式等待,以便您的测试高效并以一致的方式执行。

您必须根据您尝试点击的元素的性质来决定哪种方法最适合您,但您最终可能会使用presenceOf() 或visibilityOf()。

presenceOf() 等待将某些内容附加到 DOM

visibilityOf() 获取现有元素并等待它可见。

最后,xpath 不是一个很好的定位器。它现在可能在您当前的测试中工作,但 xpath 非常脆弱,因为随着开发的继续它很容易改变。我建议您找到另一种方法来定位该元素,因为该特定元素上的属性并不多,我可能会选择cssContainingText()

所以结合以上:

var el = element(by.cssContainingText('span', 'Number of Products'));
var EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(el), 5000);  // maximum wait of 5 seconds
expect(something).toEqual(your expected result);

【讨论】:

element.all(by.css('[md-order-by="count"]')).get(1) 这怎么能找到/html/body/div[2]/ div[3]/md-content/md-card/md-card-content/div/div/div/div/md-custom-table/div/md-table-container/table/thead/tr/th[4 ]/span 不可能根据该 xpath 为您提供定位器,我没有提及您的应用程序上的内容。使用 HTML 更新您的问题 我确实期望(el.count()).toEqual(1);在您的代码之后查看计数,而不是期望某些东西,但它说 s not function 您不能在单个元素上使用.count(),只能在ElementArrayFinder 上使用。如果要使用该功能,则需要使用.all() 定位器。将其更改为element.all(by.cssContainingText('span', 'Number of Products'));【参考方案2】:

    这是po文件

    import  element, by, ElementArrayFinder, ElementFinder  from 'protractor';
    
    export class scopePos 
      greet()    
            element(by.xpath("//scope-setup-panel/mat-expansion-panel/div/div/div/div[1]/div[2]/div[2]")).click()// click is not working here
        
    
    

    步骤定义文件

    export default function () 
        this.When(/^I verified the event scope card$/, function (next) 
          //z created an object of po file and calling 
          z.greet()         
          next();
        );
    
    

【讨论】:

以上是关于NoSuchElementError:没有这样的元素:无法找到量角器的元素的主要内容,如果未能解决你的问题,请参考以下文章

Java中的元组与类

用于在 PEP 3113 之后对没有幻数的元组进行排序的 `key` 函数

选择相距超过 30 秒的元组

将列表转换为Elm中的元组

与表示日期的元组的日期差异

Python乘以相等长度的元组