無論XPath版本如何,此解決方案都可以正常工作。首先,在你的COMMON helper類的某個地方創建一個方法。
public static string GetXpathStringForIdEndsWith(string endStringOfControlId)
{
return "//*[substring(@id, string-length(@id)- string-length(\"" + endStringOfControlId + "\") + 1)=\"" + endStringOfControlId + "\"]";
}
在我的情況,下面是在不同的版本我的產品::的控制ID
1.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_7_hylHomeLoginCreateUser
2.0 :: ContentPlaceHolderDefault_MasterPlaceholder_HomeLoggedOut_8_hylHomeLoginCreateUser
然後,你可以調用上面的方法來找到具有靜態結束字符串的控件。
By.XPath(Common.GetXpathStringForIdEndsWith("<End String of the Control Id>"))
對於其餘爲V1 & V2提到的控制ID的,我使用如下:
By.XPath(Common.GetXpathStringForIdEndsWith("hylHomeLoginCreateUser"))
整體邏輯的是,可以使用下面的XPath表達式以找到結束的控制與特定的字符串:
//*[substring(@id, string-length(@id)- string-length("<EndString>") + 1)="<EndString>"]
我該如何應用這個做CSS類名的部分匹配?我正在嘗試尋找具有「自定義標籤」類或「可定製標籤」類的標籤元素。 'By.CssSelector(「label [class $ = custom-label」)'似乎不起作用。 我可以只使用By.ClassName兩次並結合結果,但仍想知道如何使用CssSelector實現這一點。 – atlantis 2013-12-16 21:01:59