有什么方法可以使用 python selenium 获取网站的所有 "inner html text" 及其对应的坐标吗?
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 并使用 .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)
每当我尝试 .location 或 .size 属性时,我都会收到 Unicode 错误。
免责声明:我已经搜索了所有 post 所以这不是一个重复的问题。
您能否尝试获取 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)
我可以使用以下代码获取 div 个元素:
divs = driver.find_elements_by_xpath("//div")
通过遍历 div 并使用 .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)
每当我尝试 .location 或 .size 属性时,我都会收到 Unicode 错误。
免责声明:我已经搜索了所有 post 所以这不是一个重复的问题。
您能否尝试获取 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)