簡體   English   中英

有沒有辦法使用 python selenium 獲取網站的所有“內部 html 文本”及其對應坐標?

[英]Is there any way to get all the “inner html text” of a website and its corresponding coordinates using python selenium?

我可以使用以下代碼獲取 div 元素:

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

通過循環遍歷 div 和 using.text 屬性,我也可以獲取文本

代碼:

for i in divs:
            print(i.text)

但在我的用例中,我想要文本的位置和大小。 請幫忙 !!

我的代碼:

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)

每當我 try.location 或.size 屬性時,我都會收到 Unicode 錯誤。

免責聲明:我已經搜索了所有帖子,所以這不是一個重復的問題。

您可以嘗試獲取 div 的坐標而不是文本。 如下所示。

for i in divs:
     print(i.location)

編輯

因此,如果您想獲取頁面中所有文本的文本坐標,請獲取頁面中的文本元素,如下所示並獲取它們的坐標。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM