Awesome Sauce
Awesome Sauce

Reputation: 45

Can't find element

I need to find the a href element and return its contents.

Here are the contents in the source:

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

Tried printing outerHTML of the Div, which is the only element I can find:

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

But it doesn't return the href contents, only this:

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

I've tried finding the href element directly but it can't find it, CssSelector as:

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

Also tried XPath as:

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

Upvotes: 3

Views: 298

Answers (3)

Awesome Sauce
Awesome Sauce

Reputation: 45

Turns out the other elements were not getting printed because the program pulling the OuterHTML too fast, before it being generated.

Solved by using:

  System.Threading.Thread.Sleep(5000);

This way it printed everything inside the Div

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193078

The WebElement from which you are trying to extract the href attribute i.e. blablabla?subject=blebleble is within an iframe so you have to switch to the iframe first then search/find the element to extract the href attribute as follows:

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

Upvotes: 0

jaredgilmore
jaredgilmore

Reputation: 657

You need to get the attribute value via .getAttribute(value), which returns a String.

So try this:

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

Upvotes: 2

Related Questions