【问题标题】:Fetch child elements under selected element using Selenium C#使用 Selenium C# 获取选定元素下的子元素
【发布时间】:2017-01-16 13:24:49
【问题描述】:

根据以下 HTML 示例查找 ul 元素下方的元素:

<ul _ngcontent-nkg-43="" ngmodelgroup="option">
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 1</span>
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 2</span>                                                    
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 3</span>
</ul>

【问题讨论】:

    标签: c# selenium


    【解决方案1】:
     var yourParentElement = driver.FindElement(By.XPath(".//ul[ngmodelgroup='option']"));
     var children = yourParentElement.FindElements(By.XPath(".//*"))
    

    后一个调用将返回 yourParentElement 的所有子元素

    【讨论】:

    • 你可以通过CssSelector来做这个吗?
    • 这是一个迟到的回应,所以这是给所有寻求类似问题的人的。这种情况下的 cssSelector 可以通过 driver.FindElements(By.cssSelector("ul[ngmodelgroup='option'] > span")) 解决。 stackoverflow.com/a/26306203/4415253
    【解决方案2】:

    如果您尝试获取 span 元素,您可以这样做: driver.FindElement(By.Xpath(".//ul[ngmodelgroup='option']")).FindElements(By.TagName("span"));

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2012-01-04
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多