Creating an XPath expression for selecting multiple siblings of a div tag

Currently, I am working on writing an XPath expression for a specific section of code:

<div class="line info">
        <div class="unit labelInfo TextMdB">First</div>
        <div class="unit lastUnit">
                <div class="line TextMd">Second</div>

I am trying to figure out how to select the second div tag by using the first div tag and the sibling concept. The XPath expression I have tried is:

//div[contains(text(), 'First')]/following-sibling::div[2][contains(text(),'Second')]

Unfortunately, this expression is not giving me the desired result. Can you help guide me on how to properly reach the "second" div tag using the "first" div tag? Your assistance would be greatly appreciated.

Answer №1

//div[contains(text(), 'First')]/following-sibling::div[2]
--------------------------------------------------------^

At this point, there is no second "following sibling." The <div> that holds the text 'Second' is actually a direct child of the div.unit.lastUnit.

To locate it, you can use:

//div[contains(text(), 'First')]/following-sibling::div[1]/div[contains(text(),'Second')]

Another method that is more flexible and doesn't rely heavily on document structure is to specify that you want the first following <div> containing the text 'Second':

(//div[contains(text(), 'First')]/following::div[contains(text(),'Second')])[1]

However, be aware that this approach may return unexpected results as it pays less attention to the overall document hierarchy.

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

Selenium mistakenly launching an incorrect Firefox-derived browser

Recently, I switched to using the Waterfox browser, a faster 64-bit version of Firefox that shares user data with Firefox. However, I encountered an issue while trying to use Selenium, as invoking Firefox actually opened the Waterfox browser: from seleniu ...

Deactivating a form field depending on a selected radio button in Angular 2

If I have two radio buttons, with a click function called localClick for the first button to give value 1 and the second button to give value 2. <div class="ui-g-12"><p-radioButton name="group1" value="Local" (click)=localClick(1) label="Local"&g ...

Choosing the best option

I need to be able to select the size for a ring from a list of sizes. The process involves two loops: one loop to extract values from the ring_size list and another to choose individual sizes. In the second loop, I want to make sure that I am selecting th ...

Manipulate iframe input element using Webdriver and Python

At the moment, I am faced with a unique challenge. I have an iframe element that is generated dynamically and needs to be saved as a variable. The structure of this iframe is quite detailed: <iframe id="iframe39993" class="green tea" src="...foo"> ...

Offspring element overrides styling of its parent

#popBox{ width:100%; height:100%; position:fixed; left:0; top:0; border-collapse:collapse; background:black; opacity:0.8; display:none; } <table id="popBox"> <tr> <td></td> <td></td> ...

Using a .NET Web-API controller variable as a parameter in a JavaScript function

I am looking to send a "variable" from the controller to a JavaScript function. The code I have implemented is as below: <div ng-controller="faqController"> <div ng-repeat="c in categories"> <h2 onclick="toggle_visibility(&apos ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...

Is it necessary to convert SCSS into CSS in React?

I have a query about whether it is necessary to first compile our scss files into css and then import the css files into our react components, or if we can simply import the scss directly into the components? I've successfully imported scss directly ...

The functionality of the CSS transform property seems to be malfunctioning

My HTML and CSS code displays texts in two blocks within a dl element. I attempted to change the ▶ symbol to a different shape using the css transform scale property. However, some browsers only show the triangle in the first column. Why is this happenin ...

Hide the form using jQuery specifically when the login submit button is selected, not just any random button

One issue I am facing is that when a form is submitted correctly, it hides the form. However, I have added a cancel button to close the pop-up thickbox window, but instead of closing the window, it also hides the form. How can I ensure that hitting cancel ...

Prevent the utilization of <span> tags for line breaks to

When resizing my window to a smaller size, I encountered some unsightly spans that cut into new lines. To demonstrate this issue, I intentionally set the width:20px;. Can this problem be avoided? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/ ...

include a button next to the input field

As you reduce the browser window size, you may notice a different layout designed specifically for iPhone. One question that arises is how to add a button next to the search text box dynamically using JavaScript. The issue at hand is that the text box is b ...

I am experiencing some unwanted movement of divs when I hide one element and show another. Is there a way to prevent this from happening

My webpage features a dynamic div that transforms into another div upon clicking a button. Although both divs share similar properties, clicking the button causes some elements to shift unexpectedly. Strangely enough, when the height of the replacing div i ...

Gathering information from various web pages simultaneously without the need to manually navigate through each page using a webdriver

I'm new to the world of web scraping and I've successfully developed a program that allows me to extract specific, dynamic data by utilizing the selenium web driver. My current project involves scraping data from a FAQ page in order to gather in ...

Selecting options in combobox for each row in a table using Angular 2 programmatically

I am currently working on an Angular 2 application using TypeScript. In one of the tables within the application, there is a select control in one of the columns. The contents of this select control are bound to another string array. <table ngContr ...

org.openqa.selenium.WebDriverException: Unexpectedly terminated process with exit code: 0 (for GeckoDriver and Firefox version 60.2.1esr)

I'm facing difficulties in determining the correct versions of Selenium Java and server for Firefox 60.2.1esr or vice versa. The test configuration includes geckodriver 0.22.0, Windows 7 OS, TestNG 6.14.3, Selenium (server and Java) v3.14.0, and JDK 8 ...

CSS fluid divs floating on a single line

Imagine having a series of images with text below them, all wrapped in a div and centered. There are 20 of these elements with similar image sizes but varying amounts of text. The goal is to line up as many as possible on one row, each with 10px margin on ...

Origin of Class not located

"Cannot locate the Jar file for Selenium 3.0. Source not found." I encountered this problem when attempting to use certain Selenium functions such as implicitwait() or switchTo(). Any suggestions on how to resolve this issue? Click here for image descrip ...

Is it possible to use React and Material-UI to make a paper element extend to full width beyond the boundaries of a Container?

Assuming I have the following code snippet: <Container maxWidth='lg'> <Grid container spacing={3}> <Grid item xs={12} md={6} > <Image src="/img/undraw_programming_2svr.png" color='transparent' ...

Displaying limited items based on conditions in CSS

If there are 10 <p> tags, is it possible to limit them to only 5 by hiding the other 5 using CSS? Do I need to add a class five times with nth-child? Can CSS accommodate conditions for this purpose? Considering that the number of <p> tags is ...