【问题标题】:How to transform xPath to CSS Selector如何将 xPath 转换为 CSS 选择器
【发布时间】:2012-05-11 23:01:57
【问题描述】:

谁能帮我将我的 xPath 转换为 CSS 选择器?我要更改的代码是这个:

String selector = "";
    if(hasBaseCls()){
        selector += " and contains(@class, '" + getBaseCls() + "')";
    }
    if(hasCls()){
        selector += " and contains(@class, '" + getCls() + "')";
    }
    if (hasName()) {
        selector += " and contains(@name,'" + getName() + "')";
    }
    if(hasStyle()){
        selector += " and contains(@style ,'" + getStyle() + "')";
    }

    if(hasVisibleOption()){
        selector += " and count(ancestor-or-self::*[contains(@style, 'display: none')]) = 0";
    }

我正在尝试更改我的框架以使用 CSS 选择器,这是我代码中的典型构造。如果我在这个问题上看到一个有价值的答案,我想我可以管理我的大部分其他结构

【问题讨论】:

  • ancestor-or-self::*[contains(@style, 'display: none')] 没有等效的 CSS。
  • 我担心会出现这个答案 :( 其他的呢?或者可能是祖先或自我的解决方法?

标签: java xpath selenium automation css-selectors


【解决方案1】:

您可以将其部分更改为:

String selector = "";
if(hasBaseCls()){
    selector += "." + getBaseCls();
}
if(hasCls()){
    selector += "." + getCls();
}
if (hasName()) {
    selector += "[name*='" + getName() + "']";
}
if(hasStyle()){
    selector += "[style*='" + getStyle() + "']";
} 

所以选择器将类似于a.class1.class2[name*='somename'][style*='somestyle']

【讨论】:

  • 谢谢,这让我更好地理解 css 选择器,让我在更改所有 xPath 表达式时更轻松
猜你喜欢
  • 2012-09-16
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
相关资源
最近更新 更多