【问题标题】:Is there any way to get all the "inner html text" of a website and its corresponding coordinates using python selenium?有没有办法使用 python selenium 获取网站的所有“内部 html 文本”及其相应的坐标?
【发布时间】:2020-03-15 19:25:04
【问题描述】:

我可以使用以下代码获取 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 错误。

免责声明:我已经搜索了所有帖子,所以这不是重复的问题。

【问题讨论】:

  • 我不明白为什么人们在没有正确阅读问题的情况下就不必要地否决了这个问题。
  • the size 是什么意思? len?
  • 我的意思是宽度和高度。类似于 python selenium 中的 element.size 属性
  • @αԋɱҽԃαмєяιcαη 那个帖子是为了漂亮的汤,我正在寻找 python selenium 中的方法。此外,如果您正确阅读了我的问题,我想要文本的坐标而不是 div。

标签: python selenium selenium-webdriver webdriver pytest-selenium


【解决方案1】:

您可以尝试获取 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)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
相关资源
最近更新 更多