在< p&gt中获取text()元素与VBA/硒

发布于 2025-02-03 03:40:49 字数 1370 浏览 0 评论 0原文

使用Excel 2019 VBA,我正在尝试从具有此结构的网页上的段落中获取数据。

<p>
    <strong>Release Date:</strong>
    " May 30th 2022"
    <br>

    <strong>From:</strong>
    <a href=URL>Title</a>
    <br>

    <strong>Performers:</strong>
    <a href=URL1>Name1</a>, 
    <a href=URL2>Name2</a>, 
    <a href=URL3>Name3</a>
</p>

这是段落的XPath。

/html/body/div[11]/div/div/div[1]/div[1]/div/div/p[1]

要获取单个元素(“发布日期”,“从“和“表演者””),我必须用“仪器”或正则表达式来解析整个段落。

是否有一种方法可以直接用

/html/body/div[11]/div/div/div[1]/div[1]/div/div/p[1]/text()[1]

xpath 他们工作。

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]/text()")(1) - Invalid Selector

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]").Attribute("text")(1) - returns nothing

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]")(1).Attribute("text") - returns nothing

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]").text(1) - invalid procedure call

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]")(1).text - returns entire paragraph

任何建议将不胜感激。

Using Excel 2019 VBA, I am trying to get data from a paragraph on a web page with this structure.

<p>
    <strong>Release Date:</strong>
    " May 30th 2022"
    <br>

    <strong>From:</strong>
    <a href=URL>Title</a>
    <br>

    <strong>Performers:</strong>
    <a href=URL1>Name1</a>, 
    <a href=URL2>Name2</a>, 
    <a href=URL3>Name3</a>
</p>

This is the xpath for the paragraph.

/html/body/div[11]/div/div/div[1]/div[1]/div/div/p[1]

To get the individual elements ("Release Date", "From" and "Performers"), I am having to parse the entire paragraph with "Instr"s or regular expressions.

Is there a way to directly reference these elements with XPath?

For example, the "Release Date" Xpath is:

/html/body/div[11]/div/div/div[1]/div[1]/div/div/p[1]/text()[1]

I have tried to get this directly with the following but none of them work.

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]/text()")(1) - Invalid Selector

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]").Attribute("text")(1) - returns nothing

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]")(1).Attribute("text") - returns nothing

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]").text(1) - invalid procedure call

webdriver.FindElementsByXPath("//div[11]/div/div/div[1]/div[1]/div/div/p[1]")(1).text - returns entire paragraph

Any advice would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文