Is there any way to get all the "inner html text" of a website and its corresponding coordinates using python selenium?

253 Views Asked by At

I'm able to get the div elements by using this code:

divs = driver.find_elements_by_xpath("//div")

and by looping through the divs and using .text attribute I'm able to get the text as well

code:

for i in divs:
            print(i.text)

but in my use-case I want the location as well as the size of the text. Please help !!

My code:

for i in range(0,len(WEBSITES)):
        print(timestamp()) #timestamp
        print(i,WEBSITES[i]) #name of the website
        driver.get(WEBSITES[i])
        delay = 10
        time.sleep(delay)   
        img = cv2.imread(os.getcwd() + '/' + str(i)+'.png')#read the image to be inscribed


        print("getting div tags \n")
        divs = driver.find_elements_by_xpath("//div")# find all the div tags
        # anchors = divs.find_elements_by_xpath("//*")#find all the child tags in the divs

        for i in divs:
            print(i.text.location)

Whenever I try .location or .size attribute I get Unicode error.

Disclaimer: I have searched through all the post so this is not a duplicate question.

1

There are 1 best solutions below

6
On BEST ANSWER

Can you try getting the coordinates of the div rather than the text. Like below.

for i in divs:
     print(i.location)

Edit

So if you want to get the text coordinates of all text in a page, get the text elements in a page like below and get their coordinates.

textElements = driver.find_elements_by_xpath("//body//*[text()]") #Gets all text elements
   for i in textElements:
      print(i.text)
      print(i.location)