2012-12-10 43 views
3

如何從輸入字段中提取文本?我嘗試使用XPath/CSSSelector,但我得到一個空文本,因爲它是一個輸入字段。從輸入字段中提取文本 - 使用Webdriver

這裏是我的html代碼:

<div> 
    <input type="text" style="width:110px;" class="display"> 
</div> 

結果:1至50 195的行

這裏是輸入字段的屏幕截圖:

enter image description here

回答

16

您需要以獲得該字段的價值。例如:element.get_attribute("value")

+1

如果輸入元素有一個名爲「價值」定義的屬性,這將失敗。在這種情況下,你需要執行'driver.execute_script(「var val = document.querySelector(selector).value; return val;」)' – uchuugaka

1

我使用C#所以這裏是工作的完整代碼:

public string TextAttributeValueByCssSelector(By by) 
{ 
    var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30)); 
    return wait.Until(drv => drv.FindElement(by)).GetAttribute("value"); 
} 
相關問題