如何使用 XPath Selenium 和 Python 从

标签获取文本

2023-12-03

我需要用 XPath 从 a 中的文本中捕获一行<p>。我需要存储文本Content-type: text/plain; charset=us-ascii到 python 中的变量中,但我收到下一个错误:

selenium.common.exceptions.WebDriverException: Message: TypeError: Expected an element or WindowProxy, got: [object Text] {}

这是我尝试的代码:

import selenium.webdriver as webdriver

browser = webdriver.Firefox()
browser.get('https://www.w3.org/Protocols/rfc1341/7_1_Text.html')

foo = browser.find_element_by_xpath('/html/body/p[5]/text()')
print(foo)

<h1>7.1  The Text Content-Type</h1>
<p>
The text Content-Type is intended for sending material which
is  principally textual in form.  It is the default Content-
Type.  A "charset" parameter may be  used  to  indicate  the
character set of the body text.  The primary subtype of text
is "plain".  This indicates plain (unformatted)  text.   The
default  Content-Type  for  Internet  mail  is  "text/plain;
charset=us-ascii".
<p>
Beyond plain text, there are many formats  for  representing
what might be known as "extended text" -- text with embedded
formatting and  presentation  information.   An  interesting
characteristic of many such representations is that they are
to some extent  readable  even  without  the  software  that
interprets  them.   It is useful, then, to distinguish them,
at the highest level, from such unreadable data  as  images,
audio,  or  text  represented in an unreadable form.  In the
absence  of  appropriate  interpretation  software,  it   is
reasonable to show subtypes of text to the user, while it is
not reasonable to do so with most nontextual data.
<p>
Such formatted textual  data  should  be  represented  using
subtypes  of text.  Plausible subtypes of text are typically
given by the common name of the representation format, e.g.,
"text/richtext".
<p>
<h3>7.1.1     The charset parameter</h3>
<p>
A critical parameter that may be specified in  the  Content-
Type  field  for  text  data  is the character set.  This is
specified with a "charset" parameter, as in:
<p>
     Content-type: text/plain; charset=us-ascii
<p>
Unlike some  other  parameter  values,  the  values  of  the
charset  parameter  are  NOT  case  sensitive.   The default
character set, which must be assumed in  the  absence  of  a
charset parameter, is US-ASCII.

打印文本内容类型:文本/纯文本;字符集=us-ascii你必须诱导WebDriver等待为了visibility_of_element_located()您可以使用以下任一方法定位策略:

  • Using XPATH and text属性:

    driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).text)
    
  • Using XPATH and get_attribute():

    driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    Content-type: text/plain; charset=us-ascii
    
  • Note:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 XPath Selenium 和 Python 从

标签获取文本 的相关文章

随机推荐

  • OnBackPressed(软键盘打开)

    我想在软键盘打开时完成活动 我想覆盖软键盘的后退事件并完成活动 我正在使用这个 但它不起作用 有什么想法吗 public boolean onKeyPreIme int keyCode KeyEvent event if keyCode K
  • 如何通过 myplayer 将视频嵌入到 QWidget 框架中?

    是否有可能在PyQt4通过嵌入视频mpylayer into a QWidget 或进入它的子类 如果是这样 您能否提供一个最小的工作示例 有关嵌入 MPlayer 的 Qt Widget 的完整示例 请尝试qmp小部件 但这里有一个最小的
  • BlackBerry 设备存储上的 SqLite 数据库

    我正在尝试在 BlackBerry 模拟器的设备存储中创建数据库 在9500模拟器中 数据库创建成功 但创建表时出现 文件系统错误 消息 在 9700 模拟器上 数据库在创建步骤失败 是否有一个代码序列可以为所有模拟器创建数据库 我编写了以
  • 从 pandas 数据框数据透视表创建绘图

    我是Python新手 想知道如何在我使用数据透视表函数创建的数据上创建条形图 Create a pivot table for handicaps count calculation for no show people based on
  • CPython 中变量赋值是如何实现的?

    我知道 Python 中的变量实际上只是某些底层对象的引用 指针 由于它们是指针 我猜它们以某种方式 存储 或以其他方式与它们引用的对象的地址相关联 这样的 地址存储 可能发生在 CPython 实现的低层 但 我对 C 的了解还不足以从源
  • R:提高成对计算的速度

    我正在使用 R 编程语言 假设我有以下两个数据框 set seed 123 df 1 lt data frame name 1 c john david alex kevin trevor xavier tom michael troy k
  • 解析错误:语法错误,C:\wamp\www\calculator\wp-content\themes\calculator\page.php 中出现意外的“使用”(T_USE) [重复]

    这个问题在这里已经有答案了 ob start require once dompdf autoload inc php use Dompdf Dompdf use Dompdf Dompdf instantiate and use the
  • 如何在表单 Serialize() ajax 上包含提交按钮名称和值

    我遇到了麻烦 我的代码不起作用 因为我的服务器脚本端需要来自提交按钮的名称 我正在使用Ajax方法 并且正在使用data serialize 当我点击提交时 它不起作用 这是我的 JavaScript 代码 function buy pro
  • ASP.net MVC 验证在不正确的字段上突出显示和图标 Jquery

    我正在寻找一种方法来更改默认的 ASP net MVC 验证 以便不再在每个不正确的表单字段旁边放置消息 而是放置一个图标 然后我会在页面的其他位置列出错误 该图标将是一个图像 因此我需要在不正确的字段旁边渲染图像标签 除了放置一个图标之外
  • M power 查询 - 重建此数据组合

    在 M 中获取以下查询的错误消息 我尝试在合并步骤之前拆分为 2 个不同的查询 但仍然收到 这不会直接访问数据源 请重建数据组合 错误 let Source Query List from SP cols if Type Indicator
  • 如何从 Microsoft Word 文档中删除超链接?

    我正在编写一个 VB 宏来为我的工作做一些文档处理 搜索文本行并将括号内的文本放入列表 框 中 当我想删除文档中的所有超链接然后生成新的超链接 不一定在原始超链接的位置 时 问题就出现了 那么问题是如何删除现有的超链接 我当前的问题是 每次
  • 让docker容器连接到网络设备

    我正在尝试制作一些 docker 容器来容纳我的一些日常工具 但我的很多工具都依赖于能够连接到设备 通过 WiFi 来提取数据 我一直在做研究 并且很困惑试图了解需要做什么 如果有的话 来支持这种情况 我知道通常 docker 容器是服务器
  • JTextPane 追加新字符串

    每篇文章都会回答 如何将字符串附加到 JEditorPane 的问题 是这样的 jep setText jep getText new string 我已经尝试过这个 jep setText b Termination time b Cri
  • pyqtgraph滚动图:以块的形式绘制,仅显示当前窗口中最新的10秒样本

    我在使用 pyqtgraph 滚动图时遇到问题 预期成绩 预期结果与实际结果非常相似pyqtgraph 示例 滚动图 plot5 X值是时间 可以通过简单的函数生成 Y 值是随机值 每 10 秒采样一次作为一个块 每个图最多可以有 1 个数
  • 使用 Oracle 数据库的 Django InspectDB 问题

    安装cx oracle并运行inspectdb 似乎没有得到任何输出 有人可以帮忙吗 将inspectdb 与Oracle 一起使用是否存在已知问题 下面是命令和settings py python manage py inspectdb
  • Nexus 7 在 Windows 7 x64 中通过“adb devices”通过 USB 不可见

    我已经做了显而易见的事情 从最新的 Android SDK 安装了 USB 驱动程序 并在平板电脑中打开了 USB 调试 当 的时候Nexus 7连接后 设备将显示在 Windows 中装置经理作为 Android Phone Androi
  • 如何在网页中隐藏敏感信息?

    我正在尝试将一个包含测验数据 包括答案 的 json 对象从我的代码发送到 javascript 我用了 var quizJson 但问题是 我的用户足够聪明 可以使用查看源代码并揭示答案 有什么建议吗 提前致谢 唯一正确的解决方案是不以任
  • 从现有 .Net 项目自动创建 UML 图的免费工具

    是否有任何免费工具可以从现有的 Net Visual Studio 2005 项目自动创建 UML 图 如果您使用的是 Visual Studio Team Suite 2005 注意 不是 2008 我相信您可以选择将 Net 代码逆向工
  • MongoDB 将写入锁定到什么级别? (或者:“每个连接”是什么意思

    在 mongodb 文档中 它说 从版本 2 2 开始 MongoDB 对大多数读写操作在每个数据库的基础上实现了锁 一些全局操作 通常是涉及多个数据库的短期操作 仍然需要全局 实例 范围锁 在 2 2 之前 每个 mongod 实例只有一
  • 如何使用 XPath Selenium 和 Python 从

    标签获取文本

    我需要用 XPath 从 a 中的文本中捕获一行 p 我需要存储文本Content type text plain charset us ascii到 python 中的变量中 但我收到下一个错误 selenium common excep