简体   繁体   English

使用cypress从复杂层次结构中获取元素

[英]get element from complex hierarchy using cypress

I'm new to cypress.我是柏树的新手。 Lately, I'm trying to get the text variables within a website and push the elements into an array.最近,我试图获取网站中的文本变量并将元素推送到数组中。 However, I cannot get the element position correctly.但是,我无法正确获取元素位置。 I tried different ways such as get(), find() but still failed in getting CypressError: Timed out retryimg: Expected to find element:'.nav-link pr-0 d-none d-lg-block>.langSelector', but never found it我尝试了不同的方法,例如get(), find()但仍然无法获取CypressError: Timed out retryimg: Expected to find element:'.nav-link pr-0 d-none d-lg-block>.langSelector', but never found it

Here are my html:这是我的html:

...
<div class="nav-link pr-0 d-none d-lg-block">
  <div id="langSelector">
  <a data-dir="AAA" href="/AAA/example"> AAA </a>
  <a data-dir="BBB" href="/BBB/example"> BBB </a>
  <a data-dir="CCC" href="/CCC/example"> CCC </a>
  </div>
</div>
...

Here are my failed try-out:这是我失败的尝试:

  cy.get('.nav-link pr-0 d-none d-lg-block > .langSelector').find('>a').each(($el) => {
          cy.wrap($el).invoke('text')
            .then(text => {
              exampleArray.push(text.trim())
            })
        })

And another fail try-out:另一个失败的尝试:

 cy.get('div > .langSelector > a').each(($el) => {
          cy.wrap($el).invoke('text')
          .then(text => {
            exampleArray.push(text.trim())
          })
      })

the error message are the same : CypressError: Timed out retryimg: Expected to find element:'.nav-link pr-0 d-none d-lg-block>.langSelector', but never found it Any ideas would be a great help!错误消息是相同的: CypressError: Timed out retryimg: Expected to find element:'.nav-link pr-0 d-none d-lg-block>.langSelector', but never found it任何想法都会有很大帮助! Thanks million!谢谢万!

langSelector isn't a class so it shouldn't have a . langSelector不是一个类,所以它不应该有一个. in front of the selector.在选择器的前面。 Its an id so it should be selected with # .它是一个id所以应该用#选择它。

Try cy.get('#langSelector > a')试试cy.get('#langSelector > a')

Reference:参考:

As a side note, best practice would be to use a separate attribute in your html to assign a testing specific id (something like data-tid, data-cy, etc.) and reference that for your test.作为旁注,最佳实践是在您的 html 中使用单独的属性来分配测试特定的 id(例如 data-tid、data-cy 等)并在您的测试中引用它。 This info is also in the linked reference above.此信息也在上面的链接参考中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM