2017-09-13 93 views
3

我需要找到a href元素並返回其內容。找不到元素

這裏是源代碼中的內容:

<div id="responseDiv" style="background-color:#EBECED;width: 450px;"> 
<iframesrc="/iframe.asp" width="575" height="120" frameborder="0" 
marginwidth="1" marginheight="1" scrolling="no"> 
#document 
<html> 
<head> </head> 
<body marginwidth="1" marginheight="1"> 
<font size="3" style = "letter-spacing: 0pt" color="#336699" face="Arial" 
<a href="blablabla?subject=blebleble" target="_blank">[email protected]</a> 
</font> 
</body> 
</html> 
</iframe> 
</div> 
的事業部,這是唯一的元素

嘗試打印outerHTML我可以找到:

IWebElement pisso = driver.FindElement(By.XPath("//*[@id="responseDiv"]")); 
string outerHTML = pisso.GetAttribute("outerHTML"); 

但它不返回HREF內容,只有這個:

<div id="responseDiv" style="background-color:#EBECED;width: 450px;"> 

<iframe src="/iframe.asp" width="575" height="120" frameborder="0" marginwidth="1" marginheight="1" scrolling="no"> 
    &lt;p&gt;Your browser does not support iframes.&lt;/p&gt;</iframe> 

</div> 

我試過直接找到href元素,但它找不到它,Cs sSelector爲:

IWebElement pisso = driver.FindElement(By.CssSelector("body > font > a")); 

也試過的XPath爲:

IWebElement pisso = driver.FindElement(By.XPath("/html/body/font/a")); 

回答

1

你需要獲得通過.getAttribute(value)的屬性值,它返回一個String

那麼試試這個:

String hrefValue = driver.FindElement(By.CssSelector("#responseDiv body font a")).getAttribute("href"); 
+0

它返回找不到元素異常 –

+0

可見的元素中的一切:

使用解決了嗎?或者在彈出窗口中? – jaredgilmore

+1

看起來是在一個iFrame,所以你需要切換到該iFrame。 – smit9234

0

WebElement從中您正試圖提取href屬性即blablabla?subject=bleblebleiframe內,所以你必須切換到iframe第一,然後搜索/查找元素提取所述href屬性如下:

driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[@src='/iframe.asp'])); 
IWebElement pisso = driver.FindElement(By.XPath("/html/body/font/a")); 
0

原來因爲程序拉動OuterHTM並沒有獲得打印的其它元件在生成之前,L太快了。

System.Threading.Thread.Sleep(5000); 

這樣,它打印的股利