如何从具有多个相同项的父元素中获取子元素?

huangapple go评论117阅读模式
英文:

How to get child element from parent element having multiple same items?

问题

  1. 我卡在了子元素的位置。子元素的属性有多个,父元素的属性也有多个。我在定位子元素时遇到了困难。元素操作 click() 不起作用。
  2. Html
Your location and language

  1. span 标签具有多个类,div 标签具有多个相同的类。
  2. 如何在 class 上执行 click() 操作?

class="locale-selector-country-heading"

  1. 我的尝试:

cy.get('.locale-selector-country-heading').contains('Hong Kong').click()

  1. 错误:
  2. `<span class="locale-selector-country-heading">Hong Kong</span>`
  3. 这个元素 `<span.locale-selector-country-heading>` 不可见,因为其父元素 `<div.locale-selector-flag-heading-wrapper>` 具有 CSS 属性:`visibility: hidden`
英文:

I am stuck at child element location. The attributes of child elements are having multiple items, also attributes of parent element are multiple items. I am finding difficulty in locating child element. Element action click() is not working.

Html:

  1. &lt;div class=&quot;locale-selector&quot; xpath=&quot;1&quot;&gt;
  2. &lt;span class=&quot;locale-selector-heading&quot;&gt; Your location and language&lt;/span&gt;
  3. &lt;div class=&quot;locale-selector-country-wrapper&quot; data-metrics-section=&quot;Site_lvl1_LocLangSelector&quot;&gt;
  4. &lt;a href=&quot;#&quot; class=&quot;locale-selector-country&quot; aria-controls=&quot;localization-container&quot; aria-haspopup=&quot;true&quot; aria-expanded=&quot;false&quot;&gt;
  5. &lt;div class=&quot;locale-selector-flag-heading-wrapper&quot;&gt;
  6. &lt;span class=&quot;flag-header-global flag-hong-kong&quot;&gt;&lt;/span&gt;
  7. &lt;span class=&quot;locale-selector-country-heading&quot;&gt;Hong Kong&lt;/span&gt;
  8. &lt;/div&gt;
  9. &lt;span class=&quot;locale-selector-chevron-arrow&quot;&gt;&lt;/span&gt;
  10. &lt;/a&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;

span tag having multiple classes,div tag having multiple same classes.

How can I perform click() action on class ?

  1. class=&quot;locale-selector-country-heading&quot;

My attempt:

  1. cy.get(&#39;.locale-selector-country-heading&#39;).contains(&#39;Hong Kong&#39;).click()

Error:

&lt;span class=&quot;locale-selector-country-heading&quot;&gt;Hong Kong&lt;/span&gt;

This element &lt;span.locale-selector-country-heading&gt; is not visible because its parent &lt;div.locale-selector-flag-heading-wrapper&gt; has CSS property: visibility: hidden

答案1

得分: 5

错误提示您,一个父元素使整个DOM树部分无法操作。

您可以强制执行点击操作以关闭可操作性检查

  1. cy.get('.locale-selector-country-heading').contains('Hong Kong').click({force:true})

但是您的测试有点不现实 - 用户不能点击看不见的东西。

然而,有时可操作性检查是不正确的 - 如果您确实可以看到元素,请继续使用 {force:true}

否则,元素可能正在过渡到可见状态(通过CSS动画)。

在这种情况下,您可以添加一个针对可见性的重试,使用 .should('be.visible')

  1. cy.get('.locale-selector-country-heading').contains('Hong Kong')
  2. .should('be.visible')
  3. .click()

关于可操作性的更多信息,请参见与元素交互

英文:

The error is telling you that a parent element is making the whole section of the DOM tree not actionable.

You can force the click, to turn off actionability checks

  1. cy.get(&#39;.locale-selector-country-heading&#39;).contains(&#39;Hong Kong&#39;).click({force:true})

but then your test is somewhat not realistic - the user can't click something that's not visible.

However, sometimes the actionability checks are incorrect - if you can actually see the element, go ahead and use {force:true}.

Otherwise, the element may be transitioning into a visible state (via CSS animation).

In this case, you want to add a retry on the visibility with .should(&#39;be.visible&#39;)

  1. cy.get(&#39;.locale-selector-country-heading&#39;).contains(&#39;Hong Kong&#39;)
  2. .should(&#39;be.visible&#39;)
  3. .click()

There's a section on actionability here Interacting with Elements

huangapple
  • 本文由 发表于 2023年6月22日 20:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定