动态获取DIV类/ Label类的值

时间:2012-08-16 14:30:22

标签: c# selenium webdriver

我有这个:

<div class="keyinfo">
<label for="ctl00_ContentHolder_DecisionControl_ctl00_lb_ApplicationReference">Application Number:</label>

<label class="formanswer">C017206461P</label> 
</div>
<br />

每次都会更改数字C017206461P。我需要捕获它并将其存储在变量中。

Selenium webdriver中的我的C#代码:

string val1 = driver.FindElement(By.XPath("//label[@class='formanswer']")).Text;

还试过

string val1 = driver.FindElement(By.XPath("//div[*[@class='Keyinfo']]//label[*[@class='formanswer']]")).Text;

但它不起作用;它将Val1的值赋予Null。

您能否建议如何获得动态变化的值?

2 个答案:

答案 0 :(得分:0)

试试这个XPath:

"//div[contains(@class, 'keyinfo')]/label[contains(@class, 'formanswer')]"

所以要抓住文本,你会这样做:

string elementXPath = "//div[contains(@class, 'keyinfo')]/label[contains(@class, 'formanswer')]"
string wantedText = driver.FindElement(By.XPath(elementXPath)).Text;

答案 1 :(得分:0)

试试这个:

var scriptExecutor = driver as JavaScriptExecutor;
var result = scriptExecutor.ExecuteScript("return $('.formanswer').first().val();").ToString();