我使用以下代码获取使用Selenium Webdriver在选定模式下的文本的ID:
String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='Blue']/..")).GetAttribute("id");
如何传递字符串getColour
代替Blue?
谢谢
您可以通过这种方式传递字符串。试试下面的代码
string getColourin = "Red";
String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='" + getColourin +"']/..")).GetAttribute("id");
要么
使用string.Format
string xpathBefore = "//option[@selected='selected' and .='{0}']/..";
string getColourin = "Red";
string finalXpath = string.Format(xpathBefore, getColourin);
String requiredId = driver.FindElement(By.XPath(finalXpath)).GetAttribute("id");
嗨,兄弟,能否请你分享这个答案。链接 @Muzzamil
@bydukaan我早就回答了。请在那里