Selenium獲取input值的兩種方法:WebElement.getAttribute("value")和WebElement.getText()


在頁面元素的定位中,有時候需要獲取到元素的頁面顯示值,用來作為斷言。例如,我需要獲取email的值"amy1111@xxx.com"。

<input class="acxiom-input span7 inline" type="text" id="confirm_email_to_download" style="font-size: 14px;" autocomplete="OFF" value="amy1111@xxx.com">

起初我是用的

driver.findElement(By.id("confirm_email_to_download")).getText();

但是在調試過程中發現獲取不到value的值,於是換了另外一種方式

driver.findElement(By.id("confirm_email_to_download")).getAttribute("value")

此時這個value值就會獲取到。

 

當頁面的HTML是如下情況時用getText()是可以獲取到的

 <span id="current-selection">Acxiom</span>

driver.findElement(By.cssSelector("span#current-selection")).getText()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM