Locate the XPath for text that is within a div tag and adjacent to a span tag

I've searched, but couldn't find an exact match, so my question is about a low HTML code:

<div class="column1">
<div>
<div class="name">
Dynamic Name
<span class="id" title="ID">Dynamic ID</span>
</div>
</div>
</div>

I'm in need of an XPath to extract the text "Dynamic Name." Here's what I've tried so far:

1. //div/div[@class='name'] - this is returning "Dynamic Name Dynamic ID"

2. //div/span[@class='id']/preceding-sibling::text()

Since both "Dynamic Name" & "Dynamic ID" are dynamic values, splitting the text is not an option. Any help is greatly appreciated!

Answer №1

Using the following XPath expression:

normalize-space(//div[@class="name"]/text())

You should see the output "Dynamic Name" in your HTML.


I appreciate the input, however, there seems to be a syntax error when I tested it on Firepath. I'm not sure where the issue lies.

As a possible solution, try using the following XPath expression:

//div[@class="name"]/text()

This might help avoid any syntax errors you are encountering, even though it may include additional whitespace in the selected text.

Answer №2

Since it was mentioned that the XPath //div/div[@class='name'] is functioning but providing excessive information, we can extract that specific element, retrieve the innerHTML, then use string manipulation to isolate the necessary data. By splitting on the first occurrence of <, selecting the first String, and performing a trim() operation for safety, we can obtain the desired result.

driver.findElement(By.xpath("//div/div[@class='name']")).getAttribute("innerHTML").split("<")[0].trim();

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

The HtmlHelper ActionLink consistently establishes the current controller instead of allowing for the specification of another controller

I have been utilizing the HtmlHelper class to incorporate some code into my layout Navbar as a menu. Here is the code snippet: public static MvcHtmlString MenuLink(this HtmlHelper helper,string text, string action, string controller) { ...

Set the page to be rigid instead of flexible

I'm struggling to lock this webpage in place, so it doesn't resize when the browser window changes. The text keeps collapsing whenever I adjust the size of the browser, and I can't figure out what's going wrong. Can someone please help ...

When attempting to retrieve a link from the `src` attribute within an iframe element using Python-selenium, I encounter a "no such element" error

Take a look at the following HTML snippet: <div class="bg_tv col-md-12 online"> <iframe width="100%" height="460" src="https://www.youtube.com/embed/dtKciwk_si4" scrolling="OFF" frameborder="0" allowfullscreen=""></iframe></div> ...

Tips on aligning three divs horizontally within a container while maintaining a height of 100%

UPDATE: The alignment has been corrected by adding floats. However, the height still doesn't fill 100%. Check out the new look: Image Link In my footer container, I want to arrange 3 columns (colored green, white, and red for clarity). Currently, the ...

Creating dynamic animations by shifting the hue of an image on a canvas

Recently, I've been exploring the world of canvas tags and experimenting with drawing images on them. My current project involves creating a fake night/day animation that repeats continuously. After trying various approaches like SVG and CSS3 filters ...

Utilizing the power of JSF ajax with Bootstrap's data-toggle feature for radio buttons

Despite my efforts, I have not been able to find a definitive answer on SO. If this question has already been addressed, please point me in the right direction. This is what I currently have: (HTML) <span class="btn-group" data-toggle="buttons-radio" ...

Whenever I include an onClick event to a div element, the entire webpage fails to display

Currently taking on the task of developing a seat booking website. I am encountering an issue with adding an event listener to a particular div element, which should trigger a function to store the value of the div. However, upon implementing the onClick e ...

The perplexing phenomena of Ajax jQuery errors

Hey there! I'm having a bit of trouble with ajax jquery and could use some guidance. $.ajax({ type:"get", url:"www.google.com", success: function(html) { alert("success"); }, error : function(request,status,error) { alert(st ...

The code snippet for the carousel in Bootstrap does not function correctly in VSCode, yet it operates effectively on codeply.com

code: <div class = "carousel-inner"> <div class = "carousel-item active" style = "background-color:red" > <h2>I don't have to sniff other dogs anymore for love. Thanks to TinDog, ...

Writing the success function for a jQuery ajax call involves defining the actions to be taken once

Embarking on my journey to learn jQuery and web development, I am faced with the task of sending user input (username and password through a submit button) to a PHP page using .ajax and success function. Below is the HTML form code: <form id="form1"&g ...

Transforming an object into an interface and utilizing the interface methods implemented in a different location

When working with Selenium, the code below can be used to capture screenshots: WebDriver driver = new FirefoxDriver(); File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(src, new File("D:\\TestNGScreenshots ...

Calculating the size of grid thumbnails or items using the calc() function

I am currently working on building a responsive portfolio grid that spans the full width of the screen. To achieve this, I have set the width of the items using calc() and ensured that the thumbnail image fills the entire div by applying the following CSS: ...

Placing a div within a 960 grid system can sometimes result in unexpected spacing

Testing on various browsers: Chrome, IE, Firefox. I'm facing an issue where the BusinessNav ID keeps getting pushed over 3 columns. Desired page layout can be found here I could definitely hack this code to adjust the positioning. However, with so m ...

Challenges with moving images in Unslider

There seems to be an issue with the unslider-arrows on the unslider banner. The images are not sliding properly when transitioning to the next image without user input. Instead, they resize from small to full size starting at the top of the .banner div. ...

Tips to locate elements with selenium in Python by utilizing ids and classes within divs

Can anyone help me with finding an element using both id and class in a div using Selenium in Python, and then clicking on it? <div id="abc" class="xyz" style="" role="presentation"></div> This is the code I am currently using: arrow = drive ...

Obtain the full HTML code for a Facebook album

I am attempting to extract the HTML code from a Facebook album in order to retrieve the photo links. However, since not all photos load at once, cURL only retrieves the links of photos that are initially loaded. Is there a way to fetch the entire loaded H ...

Tips and tricks for seamlessly aligning a specific section of your webpage to ensure it scrolls smoothly into view

My codes are not being centered when I use smooth scrolling on a specific section of a Bootstrap carousel template. Is there an easier way to achieve this? The explanation in a post from six years ago didn't help much. Here are my codes and screenshot ...

I receive an [object HTMLCollection] as the output

I am currently working on recreating a login page and I want it so that when the button is clicked, it displays the name you entered, but instead, it returns [object HTMLCollection]. My expectation was to have the name displayed as entered. var nombre ...

React component is unable to identify prop

I'm attempting to send an array of objects from the main App.js file to a smaller component using props. However, for some reason, the prop is not being recognized within the smaller component file. https://i.stack.imgur.com/WuyFr.png https://i.stac ...

JavaScript - Modify the proposed content prior to inserting it into the input field

In my current project, I have implemented a feature using jQuery UI - v1.11.4, where an HTML textbox utilizes a JavaScript autocomplete functionality. The suggested string for the textbox is created by concatenating several columns (patient_no, patient_nam ...