2016-07-27 103 views
3

我已在Yahoo mail中創建草稿,然後轉到此草稿頁面。 現在我想使用C#和Selenium Webdriver獲得主題字段的值。 我用下面的代碼,但它返回空字符串:無法在Yahoo草稿郵件中使用Webdriver獲取草稿主題字段的值

string subjectLocator = "//*[@id='subject-field']"; 
string actualSubject = driver.FindElement(By.XPath(subjectLocator)).GetAttribute("Value"); 

使用文本屬性,而不是GetAttribute方法,也於事無補。

如何使用SeleniumWebdriverC#獲取雅虎草稿中主題字段的值?

http://prnt.sc/bye5ae - HTML代碼

+0

你試過GetAttribute(「innerHTML」)嗎? –

+0

不幸的是它沒有幫助,還有空字符串返回。 –

+0

你可以在這裏粘貼HTML代碼給我們看嗎? –

回答

1

當我看到你正在使用從學科領域獲得價值爲.GetAttribute("Value"),這裏唯一的問題是要通過屬性財產Value這應該是value意味着v應該是小寫,所以你應儘量如下: -

string actualSubject = driver.FindElement(By.Id("subject-field")).GetAttribute("value"); 

或者用WebDriverWait等到元素出現在DOM如下:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("subject-field"))); 
string actualSubject = element.GetAttribute("value"); 

我測試過它,它適用於我。

希望它有幫助... :)

+1

小寫解決了這個問題,謝謝。 –