Find the preceding element of the element that includes a specific link text

Can someone help me figure out how to click on a <td> element that precedes the <td> element with the linkText "Main" using Selenium? The element I'm looking to click is shown in the highlighted area of this image:

https://i.sstatic.net/QyHOt.png

Both the target <td> element and the one following it are located within the same <tr> element, as indicated by the image showing it with id=5.

The element corresponds to the entire row highlighted below. It is identified by the linkText "Green," similar to the element below it. Therefore, I cannot use an xpath/css selector for the <td> element I intend to click.

https://i.sstatic.net/BX244.png

My goal is to click on the highlighted "Green" element, which is why I need to target the <td> element before the one with "Main" as its linkText.

I have the following code, but I am unsure how to locate the element preceding the one it currently matches:

WebElement templateFound = driver.findElement(By.linkText("Main"));

Any assistance would be greatly appreciated. Thank you.

Answer №1

If you're looking to target specific elements using XPath, you can utilize the following-sibling axis:

//td[following-sibling::td[1]/a = 'Main']

This XPath expression will select a td element that comes after a sibling td element containing a link with the text "Main".


Alternatively, once you've identified the link by its text, you can utilize the preceding axis to target the preceding td element before the parent td:

The preceding axis includes all nodes that come before the context node in the document excluding ancestors, attributes, and namespaces.

WebElement templateFound = driver.findElement(By.linkText("Main"));
templateFound.findElement(By.xpath("preceding::td"));

Answer №2

Using XPATH should make this task simple. Assuming the td element you are looking to target contains the word Green

//a[.='Main']/../../td[.='Green']

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

Managing the appearance of one DOM element using another DOM element

Hey there, I'm currently working on an Angular Material tooltip implementation. When I hover over my span element, the tooltip appears. Now, I'm wondering how I can dynamically change the background color of the tooltip based on certain condition ...

Like button for Facebook located at the bottom of a static webpage

I am facing an issue with my web application where it does not scroll properly, especially when there is a Facebook button at the bottom. The behavior in Chrome and Firefox is inconsistent and causing some trouble. In Chrome, the div repositions correctly ...

Moving elements using CSS

Hi, I've been facing this issue with my web development since the start. Whenever I zoom in, the container also moves along. Is there any way to fix this problem? .darkslidecont{ width: 200px; height: 50px; position: relative; bac ...

The recommended style guide for HTML documentation advises using spaces within the <code> tags

While reviewing the style guide for maintaining extensive documentation of an existing system using HTML for a client, I came across a particular rule that text within a code tag should be surrounded by spaces, like so: ..., the element<code> STATE ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

There seems to be a lack of information appearing on the table

I decided to challenge myself by creating a simple test project to dive into Angular concepts such as CRUD functions, utilizing ngFor, and understanding HttpClient methods (specifically get and post). After creating a Firebase database with an API key and ...

Center an image vertically and horizontally within a div element inside a container, even if the image sizes and aspect ratio differ, while also including padding

I am looking to vertically align images with varying ratios, where the image may be larger or smaller than the content and also include padding; I have tried different solutions found here, but none seem to cover all my requirements; I specifically need i ...

Enhance web design with dynamic size pseudo elements using only CSS, no

Before the title, I've added a pseudo element with a slanted background. When the title is wrapped, it increases in height. I've been attempting to make the pseudo element adjust to the title's height, but haven't succeeded yet. I&apos ...

Vertically Center Image in a Div

I have been struggling to center an image within a div, but all the usual methods like using margin: auto; do not seem to work. I have tried various popular techniques with no success. <style> /* Add a black background color to the top navigation ...

Creating a text area for printing

When attempting to print text from a JTextArea using a printer, the text does not appear in the print file, specifically the .xps file. bprint.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { ...

My preference is for the flex box to expand in width (sideways) without increasing in height (up and down)

Is there a way for flex items to expand in width but only in height when necessary? I enjoy using flexbox, but I find it frustrating that all flex items in a row grow to the same height even when there isn't enough content to fill them, resulting in a ...

I seem to be having an issue here - my style sheet just won't load

I am having an issue with my external style sheet named style.css. style.css #topbar { background-color: red; width: 1000px; height: 40px; } body{ background-color: green; } I have tried calling this style.css from the root folder, but it's not ...

What is the best way to display CSV file data on an HTML webpage?

I am encountering an issue with displaying the data from my CSV file in an HTML page. Here is a snippet of my CSV file: number:Int,english,french,german 1,one,un,eins 2,two,deux,zwei 3,three,trois,drei 4,four,quattre,four 5,five,cinque,fuenf 6,six,six,sec ...

displaying a separate HTML file with its own distinct CSS styling

Recently, I was tasked with investigating an existing website. Currently, the website utilizes a single root html file that contains multiple iframes linking to external html pages. However, the team behind the website has noticed that this setup is causin ...

Exploring the differences between characters in Java

I have an array of strings and I am trying to locate the position of the first capital letter in each string. Does anyone have a Java sample code that could help me achieve this? import java.util.*; public class CapitalLetterPosition { public int n ...

The CSS JSON code I have written is not having any impact on the appearance of my div

<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="styles/common.css"> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script type='text/javascript'> ...

Is it feasible to extract information added post page load, potentially through JavaScript, using bs4 and requests (or selenium) in Python?

I've recently started working on a Python project and I'm exploring BeautifulSoup (bs4) and Requests for the first time. As I navigate through a webpage, I notice that all the data I need is loaded dynamically through JavaScript. How can I extrac ...

CSS - when "start" is not 1, an alternative to the "nth-child" selector

Imagine a scenario where you have a list of questions that start at 0 and you want to apply CSS styling based on their value. For instance, consider the following list... <b>How are you feeling?</b> <ol start="0"> <li>Great< ...

Issues with a responsive website not displaying correctly in the Firefox browser

I have created a carousel that is functioning correctly. However, I am encountering an issue with making it fully responsive. It appears to work properly in webkit browsers, but there are some issues in Firefox. Despite the images resizing properly, the ...

Utilizing JSON, HTML, and jQuery to create a dynamic cascading dropdown menu

Is there a way to dynamically populate a dropdown menu for states and cities using jQuery? I have a JSON file with the state-city mapping, how can I implement this? Here is an example of the JSON file: [ { "state":"Karnataka", "cities": ...