Home > Back-end >  Python selenium how to get html code from element
Python selenium how to get html code from element

Time:07-29

I wanna get a html code from a element but when it run it get error say findtable.get_attribute('innerHTML') AttributeError: 'list' object has no attribute 'get_attribute'

#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')

CodePudding user response:

instead of driver.find_elements() use driver.find_elements() without s at the end

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')


CodePudding user response:

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')
  • Related