2012-05-02 53 views
0

任何人都可以幫助我將xPath轉換爲CSS選擇器嗎?我想改變的代碼是這一個:如何將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選擇器,這是我的代碼中的典型構造。如果我看到在這一個寶貴的答案,我想我可以管理我的大多數其它結構

+1

有沒有相當於'ancestor-or-self :: * [包含(@style,'display:none')]''的CSS。 – BoltClock

+0

我很害怕這個答案會出現:(對其他人怎麼樣?或者可能是祖先或自我的解決方法? – vali83

回答

0

你可以改變它的一部分:

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']

+0

謝謝,這使我更好地瞭解css選擇器和我的生活更容易更改我的所有xPath表達式 – vali83