[英]How to get inner text of an element in Selenium?
我正在使用DOM
節點
<input type = "form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
我怎樣才能得到它的價值? 我很掙扎,因為element.getText()
不起作用並返回空白。
任何建議都會有很大幫助。
嘗試這個
WebElement element= driver.findElement(By.id("id value"));
String val=element.getAttribute("innerText")
我認為有問題的元素是<input>
元素,因此您可以像這樣使用element.getAttribute(String attribute)
方法:
String value = element.getAttribute("value");
此輸入標記被禁用,因此element.getText()
返回空白值。 請改用element.getAttribute("textContent")
。 希望這會奏效。
您在尋找輸入文本的占位符嗎? 因為你可以試試
element.getAttribute("placeholder");
您可以轉到瀏覽器->打開開發人員工具->檢查要從中獲取屬性的元素->單擊Properties
->檢查該值是否在InnerText
中
然后按照上面評論中的說明進行操作:
element_locator.get_attribute('InnerText')
有完全相同的問題! 這篇文章為我解決了這個問題: 如何在我使用的 webdriver 中獲取元素的當前內容:
element = driver.find_elements_by_xpath(
'//button[@class="size-grid-dropdown size-grid-button"]')
element.text
這有點晚了,但它可能對正在尋找類似解決方案的其他人有所幫助:它確實有效,因為我已經對其進行了多次測試:
<input type = "form-control" type="text" data-bind="textInput: EnterpriseId" disabled autocomplete="off">
在您的示例中,您沒有任何innerText
。 因此,您只能使用現有屬性獲取前面提到的屬性。 在您的情況下:類型、數據綁定、EnterpriseId 和自動完成。 由於未創建此屬性,因此不會有任何值。
如果您想獲得任何現有的,這應該沒問題:
String example= driver.findElement(ByLocator(("")).getAttribute("any attribute of your input");
System.out.println(example);
正如其他人所建議的那樣,HTML 的input
節點沒有 text 屬性,因為它們可以在value屬性中以多種格式存儲數據。 這可以在HTML input
API 規范中很容易地看到,其中該form control
可以是radio
、 date
、 file upload
等等類型。
因此,在您的特定情況下,我建議您檢查webdriver
的 API 以獲取能夠檢索value
屬性的方法。
作為評估 Selenium 中元素的 innerText 的獎勵:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("yourEl")));
wait.until(ExpectedConditions.attributeToBe(By.id("yourEl"), "innerText", yourValue));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.