Selenium for Python: Get text() of node that is shared with another element, via XPath

PAN

On this page I would like Selenium for Python to grab the text contents of the "Investment Objective", excluding the <h3> header. I want to use XPath.

The nodes look like this:

<div class="carousel-content column fund-objective">
    <h3 class="carousel-header">INVESTMENT OBJECTIVE</h3>
    The Fund seeks to track the performance of an index composed of 25 of the largest Dutch companies listed on NYSE Euronext Amsterdam.
</div>

To retrieve the text, I'm using:

string = driver.find_element_by_xpath(xpath).text

If use I the this XPath for the top node:

xpath = '//div[@class="carousel-content column fund-objective"]'

It will work, but it includes the <h3> header INVESTMENT OBJECTIVE — which I want to exclude.


However, if I try to use /text() to address the actual text content, it seems that Selenium for Python doesn't let me grab it whilst using the .text to get the attribute:

xpath = '//div[@class="carousel-content column fund-objective"]/text()'

Note that there seems to be multiple nodes with this XPath on this particular page, so I'm specifying the correct node like this:

xpath = '(//div[@class="carousel-content column fund-objective"]/text())[2]'

My interpretation of the problem is that .text doesn't allow me to retrieve the text contents of the XPath sub-node text(). My apologies for incorrect terminology.

Guy

/text() will locate and return text node, which is not an element node. It doesn't have text property.

One solution will be to locate both elements and remove the unwanted text

xpath = '//div[@class="carousel-content column fund-objective"]'
element = driver.find_element_by_xpath(xpath)
all_text = element .text
title_text = element.find_element_by_xpath('./*[@class="carousel-header"]').text

all_text.replace(title_text, '')

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

selenium in python finding an element via relative xpath

分類Dev

Python selenium: Get dynamic update of text of an element

分類Dev

XPath to find last text node in element

分類Dev

InvalidSelectorException when select element for Python Selenium with xpath

分類Dev

Scrapy xpath get text of an element that starts with <

分類Dev

Selenium Webdriver get element(Python)

分類Dev

How to use XPath to select child text after another child element

分類Dev

How to get the text of a node before its first child node by xpath?

分類Dev

Python Selenium holt Text vom Element

分類Dev

Python Selenium Cannot find element using absolute xpath div

分類Dev

Can't print a browser.find_element_by_xpath with Selenium in python

分類Dev

xPath get sibling nodes when node text contains

分類Dev

Xpath based on another element value

分類Dev

Selenium Get An Element Quickly

分類Dev

Selenium XPath - how to find immediately preceding element

分類Dev

Firefox + Selenium in python: How to interactively get an element html?

分類Dev

XPATH: selecting text node with an embedded node

分類Dev

Double condition with Xpath selenium Python

分類Dev

Selenium - find element by link text

分類Dev

Selenium Dynamic Element Python

分類Dev

Using Ruby and watir-webdriver, how does one get the XPath of an element if the text is known?

分類Dev

Python Selenium get list of all links with the same text

分類Dev

How to retrieve [object Text] via XPath?

分類Dev

How do I use selenium to scrape text from a text node within a class through Python

分類Dev

How to get all td[3] tags from the tr tags with selenium Xpath in python

分類Dev

XPath to select element based on sibling/cousin text?

分類Dev

How to convert XPath Element to plain html text?

分類Dev

get css element with space selenium

分類Dev

How to get attributes in Groovy via xpath

Related 関連記事

  1. 1

    selenium in python finding an element via relative xpath

  2. 2

    Python selenium: Get dynamic update of text of an element

  3. 3

    XPath to find last text node in element

  4. 4

    InvalidSelectorException when select element for Python Selenium with xpath

  5. 5

    Scrapy xpath get text of an element that starts with <

  6. 6

    Selenium Webdriver get element(Python)

  7. 7

    How to use XPath to select child text after another child element

  8. 8

    How to get the text of a node before its first child node by xpath?

  9. 9

    Python Selenium holt Text vom Element

  10. 10

    Python Selenium Cannot find element using absolute xpath div

  11. 11

    Can't print a browser.find_element_by_xpath with Selenium in python

  12. 12

    xPath get sibling nodes when node text contains

  13. 13

    Xpath based on another element value

  14. 14

    Selenium Get An Element Quickly

  15. 15

    Selenium XPath - how to find immediately preceding element

  16. 16

    Firefox + Selenium in python: How to interactively get an element html?

  17. 17

    XPATH: selecting text node with an embedded node

  18. 18

    Double condition with Xpath selenium Python

  19. 19

    Selenium - find element by link text

  20. 20

    Selenium Dynamic Element Python

  21. 21

    Using Ruby and watir-webdriver, how does one get the XPath of an element if the text is known?

  22. 22

    Python Selenium get list of all links with the same text

  23. 23

    How to retrieve [object Text] via XPath?

  24. 24

    How do I use selenium to scrape text from a text node within a class through Python

  25. 25

    How to get all td[3] tags from the tr tags with selenium Xpath in python

  26. 26

    XPath to select element based on sibling/cousin text?

  27. 27

    How to convert XPath Element to plain html text?

  28. 28

    get css element with space selenium

  29. 29

    How to get attributes in Groovy via xpath

ホットタグ

アーカイブ