最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

getText() returns a blank in Selenium even if the text is not hidden. I also tried JavaScript - Stack Overflow

programmeradmin1浏览0评论

I am trying to match the text and select the check box. I am trying to get the text or the innerHTML from the input tag of type checkbox.

I tried getText(); it returns blank, innerHTML, innerText returns null.

I am able to fetch the value, and all the other associated attributes of the tag, but not the text. I tried getting the text by executing the JavaScript code through Selenium, but that is also returning null or blank. How can I fix this?

The text is visible - not hidden.

I am trying to match the text and select the check box. I am trying to get the text or the innerHTML from the input tag of type checkbox.

I tried getText(); it returns blank, innerHTML, innerText returns null.

I am able to fetch the value, and all the other associated attributes of the tag, but not the text. I tried getting the text by executing the JavaScript code through Selenium, but that is also returning null or blank. How can I fix this?

The text is visible - not hidden.

Share Improve this question edited Dec 2, 2020 at 13:03 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 5, 2012 at 10:34 vchandevchande 431 silver badge3 bronze badges 4
  • Try to give some codes that you have written – Anand Commented Dec 5, 2012 at 10:55
  • Could you provide the HTML code of the element. – GirishB Commented Dec 6, 2012 at 18:11
  • With Java I was having this issue with a BR tag returning empty. Instead of .getText() I used .getAttribute("innerHTML") which will then return what I was looking for, including any HTML that is invisible or text that is hidden. – jsherk Commented Jun 9, 2014 at 16:16
  • Possible duplicate: How to gettext() of an element in Selenium Webdriver – Peter Mortensen Commented Nov 22, 2020 at 7:35
Add a comment  | 

2 Answers 2

Reset to default 19

You could try using getAttribute("textContent") or getAttribute("value")

public Collection<String> getTextValuesFromSearch(WebElement searchContext) {

    final Collection<String> collectedText = new ArrayList<>();
    final Collection<WebElement> webElementsWithText = searchContext.findElements(By. css/xpath("yourElement"));

    for (final WebElement webElement : webElementsWithText) {

        collectedText.add(webElement.getText());
    }
    return collectedText;
}
发布评论

评论列表(0)

  1. 暂无评论