Skip to content Skip to sidebar Skip to footer

Selenium Returns Empty String Instead Of The Actual Data

I am using Selenium via Python in attempts to web scrape. I'm almost where I want to be but I ran into what I am now realizing is not so small of a problem. So the element I am wor

Solution 1:

Try to use the following code:

numb.get_attribute("innerText")

Solution 2:

I think you don't select the right WebElements in your code.

I tried the following code with a similar datepicker and it was printing the expected daynumber.

days = driver.find_elements_by_xpath('//a[@class="ui-state-default"]')
daynumber = days[12].text
print(daynumber)

Solution 3:

A good thing to keep in mind

The text & "innerText" does only work with visible text

If you want to get the text of a hidden or invisible element
then "textContent" is just perfect for you!

get_attribute("textContent")

Source - https://stackoverflow.com/a/43430097/14454151


Solution 4:

Core logic for get text from WebElement

  • webElement.text
  • webElement.get_attribute("innerText")
  • webElement.get_attribute("textContent")

Full code:

def getText(curElement):
    """
    Get Selenium element text

    Args:
        curElement (WebElement): selenium web element
    Returns:
        str
    Raises:
    """
    # # for debug
    # elementHtml = curElement.get_attribute("innerHTML")
    # print("elementHtml=%s" % elementHtml)

    elementText = curElement.text # sometime NOT work

    if not elementText:
        elementText = curElement.get_attribute("innerText")

    if not elementText:
        elementText = curElement.get_attribute("textContent")

    # print("elementText=%s" % elementText)
    return elementText

Calll it:

curTitle = getText(h2AElement)

Post a Comment for "Selenium Returns Empty String Instead Of The Actual Data"