What is the method for fetching text with neither a node nor attribute using xpath in Selenium?

I am currently attempting to extract specific text from an HTML document using xpath.

The structure of the HTML is shown below:

The desired "target text" that I want to retrieve is located within a p node.

However, this "target text" does not have any special node or attribute associated with it,

it simply exists by itself within the p node.

Is there a way for me to successfully extract this text?

Additionally, I am utilizing xpath in Selenium. Unfortunately, using "text()" in my xpath query proved unsuccessful.

<p class="mean" lang="ko">
    <span class="word_class ">non-target text1 </span>
    <span class="mark">non-target text2 </span>
    target text 
</p>

Answer №1

desired content can be found within the parent p node.
To extract this content, follow these steps:
Retrieve the text of the parent element (which includes both parent and child element contents).
Remove the text from child elements.
If using Selenium, you can use the following code snippet:

parent_text = ""
all_text = driver.find_element(By.XPATH, ("//p[@class='mean']")).text
child_elements = driver.find_elements(By.XPATH, ("//*[@class='parent']//*"))
for child_element in child_elements:
    parent_text = all_text.replace(child_element.text, '')
print(parent_text)

Answer №2

To effectively target all text nodes within a p element that have more than just white space, you can employ the XPath expression

//p[@class = 'mean' and @lang = 'ko']/text()[normalize-space()]
. Keep in mind that content of the text node starts after the closing </span> tag and ends before the closing </p> tag, resulting in output like:

target text

In case you wish to eliminate leading and trailing white space, you can make use of

normalize-space(//p[@class = 'mean' and @lang = 'ko']/text()[normalize-space()])
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using iedriverserver.exe to manage Edge in IE Mode with VBA scripting

Are there any code examples available for controlling Edge in IE Mode using iedriverserver.exe in VBA? I have the exe downloaded but am having trouble adding the necessary library to tools->reference in my VBA project. If anyone has experience with this t ...

The SendKeys() function tends to overlook certain characters while inputting into a text box

Recently, I transferred my Selenium installation to a new server and encountered issues with logins in some tests. Upon investigation, it was discovered that the password field was being populated with an incorrect value, resulting in test failures. To r ...

Transformation of Python try-except (or try-catch) code block into VBA for a simple Excel-Selenium workflow

Here is a piece of code I'm working on: #Code snippet in Python try: io = myElm.get_attribute("rel") print(io) except IndexError: d_io = myElm.get_attribute("data-outcome") print(d_io) except: print ...

I'm having trouble with the formatting of the XPath, as I keep receiving an error message. Can

While specifying the XPath for elements that selenium should click, do I need to provide the full path? An error is currently being thrown at this point: IWebElement gridElement = driver.FindElement(By.XPath("//div[@class='sc-80905d34-2 gGhuiq&ap ...

Failing to verify the presence of specific text within a dropdown menu using Selenium

Previously, I successfully implemented this code, however, the HTML/CSS for the dropdown has since changed and now I am unable to get it to function correctly. Below is the structure for the dropdown code, with specific text highlighted that I am trying t ...

Launching a blank webpage by employing Selenium and a web driver

I'm trying to create a webpage using chromedriver and here is the code I have so far: from selenium import webdriver url = "wow.com" driver = webdriver.Chrome("/Users/macbook/Desktop/chromedriver") driver.get(url) But when I run it, this is what h ...

The issue of JMeter WebDriver failing to recognize variable values

I'm currently facing some confusion while using JMeter with Selenium. My scenario involves having two Webdriver Samplers for different pages and a JSR223 Sampler to set up data, such as vars.put("loanAmount", "50000"); and other variables. Interesti ...

Searching for a hyperlink WebElement with Java and Selenium: Finding the way to locate it

I have been attempting to click on the "sign out" link in Gmail, but my console keeps stating that it cannot find the element. Here is the code I'm using. Thank you! @FindBy(linkText="Sign out") WebElement logoutLink; This is the HTML: view image de ...

What is the best way to verify that a page has successfully reloaded after clicking a link that navigates to the current page?

I have encountered a peculiar issue that I am attempting to replicate through testing. While I have identified the source of the problem and know how to resolve it, I am struggling to create an effective test case for it. Here is the scenario: I have my ...

What is the best way to set up a full class in C# using the PageObject design pattern?

Since the pageFactory is now deprecated in C#, I am curious about how to properly initialize classes in C#. I attempted to use @Findby to call them in the Testcase, but it resulted in a large number of objects being written. Is there a more efficient way? ...

Getting the stack with Python selenium webdriver

I tried running this code snippet to test Selenium, but encountered an issue where the Firefox window did not open and no error messages were displayed. The print statement was also not executed. from selenium import webdriver driver = webdriver.Firefox ...

How can we most effectively test XML responses?

When it comes to testing webpages with xml responses, using Selenium IDE can be a challenge. While some opt for Selenium Remote Control or Pearl modules like WWW::Mechanize and Test::XML/Test::XPath, these may not be feasible options for those who prefer J ...

I found this helpful guide on connecting to a Chrome browser that has been manually opened using the debuggerAddress method

After referencing Justin Ko's article on attaching the Chrome browser, I tried implementing the same method that had previously worked for me. However, when running the following code, I encountered an error message. require 'watir' brow ...

Step-by-step guide on choosing a dropdown menu within a submenu using Selenium WebDriver

I am a beginner with Selenium and I'm attempting to choose an option (Job Title) from the sub menu (Job) under the main menu 'Admin' on OrangeHRM website. When running my script, it correctly clicks on Admin but instead of hovering over "Jo ...

The hash for the JSONObject could not be located

Currently, I am in the process of developing a Test Automation Script using JAVA and Selenium WebDriver. The test is being executed on a cloud environment provided by crossbrowsertesting.com. One of the features allows for taking snapshots of the browser w ...

Retrieving the text value from a specialized attribute

I have a custom attribute named upgrade-test="secondary-pull mktg-data-content in the snippet of code below: <section class="dvd-pull tech-pull-- secondary-pull--anonymous tech-pull--digital secondary-pull--dvd-ping tech-pull--minimise" u ...

A guide to pressing the combination of shift + ctrl + s with Selenium

Is there a way to simulate pressing shift + ctrl + s in Selenium? I attempted it with the following code: Actions action = new Actions(driver); action.sendKeys(Keys.chord(Keys.SHIFT + Keys.CONTROL + "s")).perform(); However, an error is being thrown. ...

How to navigate to the "not now" button in Instagram notifications with Selenium and Python

I'm currently working on a custom "Instagram bot" to log in to Instagram. Although I've managed to bypass the login screen, I'm encountering an issue with a popup message that says: https://i.stack.imgur.com/OMA6h.png from selenium import we ...

Guide on extracting the text from the <script> tag using python

I'm attempting to extract the script element content from a generic website using Selenium. <script>...</script> . url = 'https://unminify.com/' browser.get(url) elements = browser.find_elements_by_xpath('/html/body/script[ ...

What could be causing the Docker Container to be unable to access localhost port 4444?

I have containerized an application that serves as the test driver for an automated Selenium test. The Selenium server (also known as Selenium Hub) is running in a separate Container, along with the Firefox Node, on localhost:4444. However, my application ...