Selenium getText

前端 未结 4 1715
庸人自扰
庸人自扰 2020-12-20 16:15

I want to getText() using By.id or By.cssSelector.

I managed to solve my problem by doing getAttribute("value"), but I don\'t understand why getText() doesn

相关标签:
4条回答
  • 2020-12-20 16:59

    Simple answer - it's designed this way. getText() parses the content of the tag (i.e. its innerText), which is obviously empty for inputs.

    0 讨论(0)
  • 2020-12-20 17:04

    You may use this if you want to search for a given text on a WebElement. Pass it directly or through a string:

    String textoToSearch = "Text inside Webelement";
    driver.findElement(By.id("someID).getText().contains("textToSearch");
    
    0 讨论(0)
  • 2020-12-20 17:06

    http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getText()

    getText() delivers the innerText of a WebElement.

    Your input field does not have any inner Text. The text is found inside your value-attribute, hence accessing it via getAttribute("value") is the correct way to do it.

    0 讨论(0)
  • 2020-12-20 17:14

    Java ele.getAttribute("innerHTML");

    This could get the text already in the background and not displayed on the page yet.

    0 讨论(0)
提交回复
热议问题