2

With Selenium, why does the following function print empty string? Same if I try 'innerText' instead of 'innerHTML'.

send_keys part works fine though.

def button_clicked(self):
        browser = webdriver.Firefox()
        browser.get('https://www.google.com')
        search_box = browser.find_element_by_xpath("//input[@title='Search']")
        search_box_HTML = search_box.get_attribute('innerHTML')
        print(search_box_HTML)
4
  • Can you sum up your Manual Steps which you are trying to Automate? Commented Feb 18, 2018 at 14:05
  • This code is just to put the print statement in some context. What I am interested in is why does innerHTML` and innerText not work with Python in Selenium. For example, when automating IE through VBA I can print innerHTML with getAttribute("innerHTML") without a problem. Commented Feb 18, 2018 at 15:52
  • Won't comment about innerText() but getAttribute("innerHTML") is proven, powerful and working when used in proper context. However I don't understand the line/xpath find_element_by_xpath("//input[@title='Szukaj']") from your code while I can't see the context of textbox in your code as well. Commented Feb 18, 2018 at 16:03
  • I have removed the 'textbox' line as irrelevant to the problem. find_element_by_xpath("//input[@title='Search']") locates the input box on Google. I want to print its innerHTML. Commented Feb 18, 2018 at 16:31

3 Answers 3

2

An <input> has no inner content—text or HTML. Instead, the data entered by the user is stored in its value attribute, which can be retrieved with WebElement#get_attribute():

search_box.get_attribute(“value”)
Sign up to request clarification or add additional context in comments.

Comments

1

If you look at the HTML of the page https://www.google.com and inspect the Search Box WebElement which you have identified as :

find_element_by_xpath("//input[@title='Search']")

The WebElement is defined as follows :

<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" type="text" value="" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" spellcheck="false" style="border: none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) transparent; position: absolute; z-index: 6; left: 0px; outline: none;">

get_attribute("innerHTML")

As per the documentation, Element.innerHTML gets the HTML syntax describing the element's descendants and get_attribute() is defined as follows :

    def get_attribute(self, name):
    """Gets the given attribute or property of the element.

    This method will first try to return the value of a property with the
    given name. If a property with that name doesn't exist, it returns the
    value of the attribute with the same name. If there's no attribute with
    that name, ``None`` is returned.

    Values which are considered truthy, that is equals "true" or "false",
    are returned as booleans.  All other non-``None`` values are returned
    as strings.  For attributes or properties which do not exist, ``None``
    is returned.

    :Args:
        - name - Name of the attribute/property to retrieve.

    Example::

        # Check if the "active" CSS class is applied to an element.
        is_active = "active" in target_element.get_attribute("class")

    """

Hence, as the WebElement i.e. Search Box doesn't have a descendant, so get_attribute('innerHTML') returns empty

1 Comment

@barciewicz Upvote this/any Answer which is/was useful to you for the benefit of future readers.
0

You should change your code to:

search_box.get_attribute('value')

That should do the trick

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.