Using WebDriver Selenium to choose an element within a table following another element

The webpage features a user details table with a functionality to delete users. To remove a user, I must select the row based on the username and then click the "Delete" button. The structure of the HTML table is as follows:

<table id="tblUsersGrid" cellpadding="2">  
<thead>  
<tbody>
<tr class="aboutMeRow" data-parentid="223">  
<tr>  
<tr>  
 ...
<tr>
    <td class="cell cell_278 cell_NameCell">xoxo</td>
    <td class="optionIcon overrideFloat orgIcon cell cell_278 gridCell"></td>  
    <td class="cell cell_278 gridCell">Custom</td>  
    <td class="cell cell_278 gridCell">qaadmin</td>  
    <td class="cell cell_278 gridCell">0</td>  
    <td class="cell gridCell">  
        <div class="removeAccountIcon"></div>  
    <td class="cell gridCell">  
        <div class="editAccountIcon"></div>  
    </td>  
    <td></td>  
</tr>  

To easily locate the desired row, I can use the following XPath:

driver.findElement(By.xpath("//td[@class='cell_NameCell'][contains(text(),'xoxo')]"))  

However, I am unsure how to access the removeAccountIcon element within that same row. While there are solutions using XPath for similar scenarios, I'm wondering if there is a CSS selector alternative to achieve this task rather than solely relying on XPath.

Answer №1

If you want to get it done in one shot, you can use the following XPath query:

//tr[td[contains(@class, 'cell_NameCell')] = 'xoxo']//div[@class='removeAccountIcon']

In this scenario, we find the right tr row by examining the text within the td element that has a class of cell_NameCell (which represents your username cell). Next, we pinpoint the "Remove Account Icon" located within this specific row.

Answer №2

If you're dealing with Xpath, using the preceding-sibling axis can be beneficial in solving your problem. Here's a sample xpath to try out:

//td[preceding-sibling::td[contains(text(),'xoxo')]][5]/div[@class='removeAccountIcon']

Please update me on whether this solution works for you.

Answer №3

If you want to choose the right row, try using findElement on that specific row instead of the main driver to access the element within it. It seems like the code proposed for locating the correct row is not functioning as expected because it's selecting a td instead of a tr.

Here's an alternative approach:

WebElement userDataInRow = driver.findElement(By.xpath(//td[contains(text(),'xoxo')]))
WebElement row = userDataInRow.findElement(By.xpath(".."))
row.findElement(By.xpath("//div[@class='removeAccountIcon']"))

The second line navigates to the parent element of the td element that was chosen.

Answer №4

It doesn't seem possible to locate the parent using CSS selectors. However, there are alternative methods mentioned in this article that can be used for the same purpose.

If you want to find the "Remove" button using xpath, you can simply navigate back to the parent of the table cell containing the "Name" and then select the "div" element of the remove link/button.

In this case, your modified xpath should look like this:

//td[contains(text(),'xoxo')]/../td/div[@class='removeAccountIcon']

This xpath will successfully capture the "Remove Account Icon" based on the provided name information.

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

Guide on determining the total number of rows within a given table

Let's say I want to count the number of rows in a table, but there is no ID available and a nested table is used. How can I write an XPath to count the number of rows? WebDriver driver = new FirefoxDriver(); driver.get("http://newtours.demoaut.com/") ...

Discrepancies arise when attempting to extract specific specifications from a particular website

Option Explicit Private MB As Selenium.ChromeDriver Sub Test() Dim i As Long Dim lastrow As Long lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row For i = 2 To lastrow Dim MyURL As String MyURL = Sheet1.Cells(i, 1).Value Set MB = New Selenium ...

Encountering a problem while trying to log into Instagram with selenium, receiving the error message "We had trouble logging you in to Instagram."

I have been working on a code to scrape Instagram, search, and analyze posts and likes. My bot was functioning perfectly fine until yesterday when it suddenly stopped being able to log in. I've tried various troubleshooting methods: Logging out from ...

Header with Sticky Behavior and Smooth Slide Down Animation

I am trying to achieve a scroll effect on my page where the header will move up when scrolling up, but at position 150 it should smoothly slide down and stay fixed at the top. I have tried various methods but haven't been able to get it right. Can som ...

Using Selenium Web Driver to interact with buttons within Eclipse IDE

Can Selenium Web Driver be used to launch an IDE and click a button within it? I have a website where clicking a button triggers actions in Eclipse IDE. Selenium Web Driver typically interacts with web content, but is there a way to integrate it with Ecl ...

Steps for setting up Dependency in POM.XML for Testlink (testlink-api-client) 2.0

As part of my current research project, I needed to incorporate a Selenium (Java) based automation framework with TestLink for updating test cases. While I was successful in achieving this goal, there were issues when others tried to use the Java Package i ...

What is the best way to align a checkbox and text in the same row within a Mailchimp embedded form?

I am troubleshooting a styling issue with my Mailchimp form that has groups. The problem I'm encountering is that the checkboxes are displaying on one line while the text appears on the next line in an unordered list format. To see the issue for yours ...

Determining the decimal width of an element using jQuery

I've been searching everywhere for a method to accurately determine the width of an element without rounding. The reason behind this is that I have multiple elements floated to the left with display inline-block and I am attempting to automatically ad ...

Issue with xPath not being able to find and input data into a field when using tools like selenium and Webdriver

For the past few days, I've been diving into xPath and selenium in my research but can't seem to find a solution to my problem. Following tutorials on locating text fields through 'inspect' on websites and using send_keys() on them, I e ...

Styling JavaFX ChoiceDialog with CSS

I'm currently struggling with customizing the appearance of a ChoiceDialog in my JavaFX application using CSS. While I've successfully applied styles to other dialog panes, it seems that the ChoiceDialog is not responding as expected, possibly du ...

Rotating and moving two boxes using CSS3 transformations

I have two boxes, each measuring 200px by 200px, that I would like to animate using CSS animations. The first box (upper box) should rotate from rotateX(0deg) to rotateX(90deg) with a transform-origin of center top. The second box should follow the bottom ...

What is the process to include a CSS file in PHP?

Can anyone guide me on how to include my CSS file in PHP? require('config.php'); $cssFile = "style.css"; echo "<link rel='stylesheet' href='/books/style.css'>"; What is the process of adding CSS to a PHP appl ...

Combining an image and a card in Bootstrap 5: a step-by-step guide

Is there a way I can align an image and a card in the same line like in this example? I want the image and card to be combined in one line, but when the page is viewed on mobile devices such as Android or iOS, I want the image to appear at the top with th ...

Tips for keeping two row headers in place on a table while scrolling:

Check out my fiddle here: https://jsfiddle.net/oed1k6m7/. It contains two td elements inside the thead. thead th { position: -webkit-sticky; /* for Safari */ position: sticky; top: 0; } The code above only makes the first row fixed. How can I make b ...

Adjust the color of the text in Ionic based on the condition

My current solution involves highlighting text in a small slider after a user tap, but it feels very makeshift. <ion-slide *ngFor="let loopValue of values"> <div *ngIf="viewValue == loopValue"> <b> {{loopValue}} </b> ...

Experiencing difficulties when attempting to show or hide elements using jQuery

I spent several hours trying to figure this out and couldn't get it right. Is it feasible to create a jQuery script that will perform the following action when a <span> element is clicked: Check if any of the <p> elements are current ...

In my selenium test, what is the best method to drag and drop an element to adjust its offset specifically for FireFox compatibility?

While running a Selenium test on FireFox browser to move an element using the DragAndDropToOffset function in the Actions Class, I encountered an exception like this: System.InvalidOperationException: data did not match any variant of untagged enum Pointer ...

Skipping BDD tests in Terminal Serenity due to unexpected errors

Having trouble running tests in Terminal for Serenity BDD, specifically using the command ( mvn verify serenity:aggregate ). Can anyone provide some guidance or assistance with this issue? Attached a screenshot for reference Your help would be greatly ap ...

What is the best way to eliminate the left padding on a TimelineItem component?

When using the Timeline component to display information, there are three columns: TimelinItem, TimelineSeparator, and TimelineContent. I tried setting the padding of TimelineItem to 0 but it didn't work. The <Trace/> component is a sub-compone ...

What are the steps for setting up Geckodriver on my computer?

When it comes to utilizing Selenium in Python, I find myself at a loss when faced with the information provided on https://pypi.python.org/pypi/selenium To make use of Selenium, a driver is required to communicate with the chosen browser. For instance, Fi ...