2016-02-02 119 views

回答

1

您可以使用XPath

span = driver.findElement(By.id("lblName")); 
div = span.findElement(By.xpath("..")); 
+0

我不知道爲什麼,但我不能夠通過ID – Oak

+0

定位元素中找到該iframe是造成不被發現的元素 – Oak

1

你可以試試://span[@id = 'lblName']/parent::div/text()

0

事情是這樣的:

string xml = "<?xml version=\"1.0\"?>" + 
      "<div>" + 
      "<span id=\"lblName\" class=\"fieldTitle\">Name</span>" + 
      "</div>"; 

XDocument xdoc = XDocument.Parse(xml); 

var parent = xdoc.Descendants().First(el => el.Name == "span" && 
         el.Attribute("id") != null && 
         el.Attribute("id").Value == "lblName").Parent; 
0

您不能硒獲取文本節點。請嘗試以下JS的解決方法:

IWebElement span = driver.FindElement(By.Id("lblName")); 
IWebElement div = span.FindElement(By.XPath("..")); 
string script = "var nodes = arguments[0].childNodes;" + 
       "var text = '';" + 
       "for (var i = 0; i < nodes.length; i++) {" + 
       " if (nodes[i].nodeType == Node.TEXT_NODE) {" + 
       "  text += nodes[i].textContent;" + 
       " }" + 
       "}" + 
       "return text;"; 
string text = driver.GetJavaScriptExecutor().ExecuteScript(script, div).ToString(); 
相關問題