[英]Can't find element
I need to find the a href
element and return its contents. 我需要找到
a href
元素并返回其内容。
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">merkl@gmail.com</a>
</font>
</body>
</html>
</iframe>
</div>
Tried printing outerHTML of the Div, which is the only element I can find: 尝试打印Div的outerHTML,这是我能找到的唯一元素:
IWebElement pisso = driver.FindElement(By.XPath("//*[@id="responseDiv"]"));
string outerHTML = pisso.GetAttribute("outerHTML");
But it doesn't return the href contents, only this: 但它不会返回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">
<p>Your browser does not support iframes.</p></iframe>
</div>
I've tried finding the href element directly but it can't find it, CssSelector as: 我已经尝试直接找到href元素,但它无法找到它,CssSelector为:
IWebElement pisso = driver.FindElement(By.CssSelector("body > font > a"));
Also tried XPath as: 还试过XPath:
IWebElement pisso = driver.FindElement(By.XPath("/html/body/font/a"));
You need to get the attribute value via .getAttribute(value)
, which returns a String
. 您需要通过
.getAttribute(value)
获取属性值,该.getAttribute(value)
返回一个String
。
So try this: 试试这个:
String hrefValue = driver.FindElement(By.CssSelector("#responseDiv body font a")).getAttribute("href");
The WebElement
from which you are trying to extract the href
attribute ie 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: 您尝试从中提取
href
属性的WebElement
,即blablabla?subject=blebleble
在iframe
因此您必须先切换到iframe
然后搜索/查找元素以提取href
属性,如下所示:
driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[@src='/iframe.asp']));
IWebElement pisso = driver.FindElement(By.XPath("/html/body/font/a"));
Turns out the other elements were not getting printed because the program pulling the OuterHTML too fast, before it being generated. 事实证明其他元素没有被打印,因为程序在生成之前过快地拉动OuterHTML。
Solved by using: 通过使用解决:
System.Threading.Thread.Sleep(5000);
This way it printed everything inside the Div 这样它就可以打印Div中的所有内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.