Search code examples
pythonseleniumselenium-webdriverinnerhtmlgetattribute

Python Selenium: How to get the innerHTML from element


I want to get get the innerHTML from a element but when it run it get error say:

findtable.get_attribute('innerHTML')
AttributeError: 'list' object has no attribute 'get_attribute'

Code trials:

#findtable
findtable = driver.find_elements(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td[1]/center/img")

findtable.get_attribute('innerHTML')

Solution

  • find_elements* returns a list, where as to identify a single element you need find_element*.

    Additionally, get_attribute() is a WebElement method.


    Solution

    You need to change driver.find_elements() as driver.find_element(), you'll be good to go.

    Your effective code block will be:

    findtable = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div/table/tbody/tr[1]/td[1]/center/img")
    findtable.get_attribute('innerHTML')