簡體   English   中英

Element.getattribute(“ Value”)和gettext()不適用於使用Java的Selenium Webdriver

[英]Element.getattribute(“Value”) and gettext() is not working for selenium webdriver using Java

我正在嘗試從元素中獲取值或文本,但是它不起作用。 請幫助我。 以下是路徑和Web元素。

鏈接: https//www.tcsion.com/OnlineAssessment/ScientificCalculator/Calculator.html

元件:

<input id="keyPad_UserInput" class="keyPad_TextBox" maxlength="30" readonly="" type="text">

您可以使用以下代碼從文本字段獲取價值。

driver.findElement(By.id("keyPad_UserInput")).click();              
String text=driver.findElement(By.id("keyPad_UserInput")).getText();
System.out.println(text);

嘗試這個; driver.findElement(By.id( “keyPad_UserInput”))。的getAttribute( “值”)

您可以使用javascript執行程序從輸入字段獲取值。

String return_value = (String) js.executeScript("return document.getElementById('keyPad_UserInput').value");

希望對您有所幫助!

使用以下代碼,您將在字段中輸入文本。

driver.findElement(By.xpath(".//*[@id='keyPad_btn2']")).click();
WebElement firstName = driver.findElement(By.xpath(".//*[@id='keyPad_btn2']"));
    String value = firstName.getText();
    System.out.println(value);

輸入類型為只讀,您將僅通過使用getAttribute("value")方法獲取數據

    driver.get("https://www.tcsion.com/OnlineAssessment/ScientificCalculator/Calculator.html");
                System.out.println("  Page");
                Thread.sleep(3000);
                WebElement ele = driver.findElement(By.xpath("//input[@id='keyPad_UserInput']"));
                System.out.println("value using getAttribute ::"+ele.getAttribute("value"));
                System.out.println("value using getText::"+ ele.getText()); //its return empty or null 

輸出

 value using getAttribute :: 0
 value using getText      :: 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM