【问题标题】:How to Create a dynamic xpath which have only styles no class or id如何创建只有样式没有类或 id 的动态 xpath
【发布时间】:2020-01-25 07:06:09
【问题描述】:

我怎样才能为这个elemant 创建一个动态XPath,这里没有class 或id,只是内联CSS。我也需要像这样增加 div 号

<div role="none" style="position: relative; display: flex; flex-direction: row; flex-grow: 1; flex-shrink: 1; overflow: hidden; align-items: center; border-radius: 0px; cursor: pointer; padding: 4px 10px; background-color: rgba(0, 0, 0, 0); min-height: 55px;"><div role="none" style="position: relative; display: flex; flex-direction: row; flex-grow: 1; flex-shrink: 1; overflow: hidden; align-items: center; background-color: rgba(0, 0, 0, 0);"><div role="none" aria-hidden="true" style="position: relative; display: flex; flex-direction: column; flex-grow: 0; flex-shrink: 0; overflow: visible; align-items: stretch; width: 40px; height: 40px; justify-content: center;"><div role="none" style="position: absolute; display: flex; flex-direction: column; flex-grow: 0; flex-shrink: 0; overflow: hidden; align-items: center; left: 0px; right: 0px; top: 0px; bottom: 0px; justify-content: center;"><div role="none" style="position: relative; display: flex; flex-direction: row; flex-grow: 0; flex-shrink: 0; overflow: hidden; align-items: center; background-color: rgb(229, 228, 232); width: 40px; height: 40px; border-radius: 20px; justify-content: center;"
/html/body/div[1]/div/div[1]/div[2]/div/div[1]/div/div[1]/div[4]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[2]/div/div

/html/body/div[1]/div/div[1]/div[2]/div/div[1]/div/div[1]/div[4]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[3]/div/div

【问题讨论】:

    标签: c# selenium selenium-webdriver automation automated-tests


    【解决方案1】:

    在 DOM 中,您可以找到任何具有 id/ 类名的父级。 ' 那你就可以亲子关系了。

    格式://标签名[@locator='locatorvalue']

    对具有 id 或类名的父级使用上述格式,然后导航到相应的子级

    //标签名[@locator='locatorvalue']/childtagename1/childtagename2

    //tagname[@locator='locatorvalue']//childtagename5

    【讨论】:

    • 它一直在改变 id 兄弟!
    • 对于它正在改变的 webelement,父 id/classnames 呢。请分享你想要选择的 DOM 和 web 元素的屏幕截图
    【解决方案2】:

    尝试创建格式化字符串,然后在其中放入您想要的内容

    string searchvalue = "position: relative; or whatever"
    Driver.FindElement(By.XPath(string.Format(".//div[contains(@style,'{0}')]", searchvalue)));
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2011-11-13
      • 2021-09-04
      相关资源
      最近更新 更多