【问题标题】:What is the difference between 'page_source' and 'find_element_by_tag_name("body").text'?'page_source' 和 'find_element_by_tag_name("body").text' 有什么区别?
【发布时间】:2021-05-10 17:53:27
【问题描述】:

尝试查找 UI 登录页面(网页)上是否存在文本。 我可以通过'driver.page_source()'driver.find_element_by_tag_name("body").text 验证它

  1. driver.page_source()

    text = "abcd"
     page_source = driver.execute_script("return document.body.innerHTML;")
     if text in page_source:
         return True
     else:
         return False
    
  2. driver.find_element_by_tag_name("body").text

    text = "abcd"
     value = text in self.browser.find_element_by_tag_name("body").text
     if value:
        return True
     else:
        return False
    
  1. method1和method2有什么区别?
  2. 哪一个更适合完成所需任务?
  3. 哪个更快?
  4. 或者要使用任何 Selenium-UI 方法?

任何帮助将不胜感激。寻找有价值的投入。 对此有任何想法吗?这里有什么帮助吗?

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    页面源将提供所有文本,包括 HTML 标记、样式等,就像您在执行脚本中编写的一样,返回 innerHTML。因此,将返回所有 HTML 代码,其中显然也包含文本。您也可以使用 selenium 获取整个 html,而不是通过 browser.page_source 使用 JavaScript 执行器。

    另一方面,browser.find_element_by_tag_name("body").text 将返回您在页面上看到的所有没有 html 标记的文本。

    对我来说,第二种方法应该是首选且速度更快,因为您将获得长度更短的字符串(没有不必要的 html 标签)和您感兴趣的实际文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 2022-12-04
      • 1970-01-01
      • 2016-05-23
      • 2015-10-24
      • 2022-07-21
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多