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

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

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

问题

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

Html:

Your location and language


span 标签具有多个类,div 标签具有多个相同的类。

如何在 class 上执行 click() 操作?

class="locale-selector-country-heading"


我的尝试:

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


错误:

`<span class="locale-selector-country-heading">Hong Kong</span>`

这个元素 `<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:

&lt;div class=&quot;locale-selector&quot; xpath=&quot;1&quot;&gt;
&lt;span class=&quot;locale-selector-heading&quot;&gt; Your location and language&lt;/span&gt;
&lt;div class=&quot;locale-selector-country-wrapper&quot; data-metrics-section=&quot;Site_lvl1_LocLangSelector&quot;&gt;
   &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;
       &lt;div class=&quot;locale-selector-flag-heading-wrapper&quot;&gt;
           &lt;span class=&quot;flag-header-global flag-hong-kong&quot;&gt;&lt;/span&gt;
           &lt;span class=&quot;locale-selector-country-heading&quot;&gt;Hong Kong&lt;/span&gt;
       &lt;/div&gt;
       &lt;span class=&quot;locale-selector-chevron-arrow&quot;&gt;&lt;/span&gt;
   &lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;

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

How can I perform click() action on class ?

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

My attempt:


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树部分无法操作。

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

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

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

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

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

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

cy.get('.locale-selector-country-heading').contains('Hong Kong')
  .should('be.visible')
  .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

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;)

cy.get(&#39;.locale-selector-country-heading&#39;).contains(&#39;Hong Kong&#39;)
  .should(&#39;be.visible&#39;)
  .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:

确定