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

Switch the background color of the body when hovering over a link

Is it possible to change the page background when hovering over a a using only CSS? I am searching for a solution that does not involve any JavaScript. I understand that we can access child elements with CSS, but I am unsure if it is possible to target th ...

Discovering a particular element involves iterating through the results returned by the findElements method in JavaScript

I am attempting to locate and interact with a specific item by comparing text from a list of items. The element distinguished by .list_of_items is a ul that consists of a list of li>a elements. I am uncertain about how to transfer the determined elemen ...

Slowly zooming background image

I've been searching all over the place but can't seem to find a clear answer on how to slowly zoom in the background image using CSS. Any help would be greatly appreciated. Currently, my code only zooms the text elements and not the background. ...

Obtaining the class value using the getAttribute function

Is there a way to extract and store the value of the class (for example, "AAABC") in a variable? I've attempted various keywords with the getAttribute method, but have had no success. Using terms like "class" returned "gwt-Label", while others yielded ...

How can you prevent the automatic reset of sessions between scenarios in Behat Mink?

Our company website includes statistics from Facebook, and users log in using their Facebook accounts to access this information. I have written BDD tests that log into the site and test its functionality, however, we are facing issues with frequent loggin ...

Tips for selecting a Checkbox within a table generated by ng-repeat using Selenium

For my Selenium Java automation of a web application, there is an angularJS ng-repeat table with checkboxes in each row that I need to select based on certain conditions for testing. I have tried the following code snippet to locate and interact with thes ...

Do I have to cram all the content onto a single page just to use a scroll effect?

I'm currently facing a dilemma as I work on building my portfolio. My goal is to primarily use html5 and css3, along with a bit of js, jquery, and other tools if necessary. Although I am not an expert in web development, I wanted to push myself to cre ...

Struggling to interact with a radio button using Selenium WebDriver

I am attempting to select a radio button using Selenium. The button is located in the center. https://i.stack.imgur.com/UDu7r.png I am struggling to figure out how to click on the button, as all of the attributes for this element are contained within the ...

The escape character appears to be misunderstood, causing an error due to an invalid escape sequence

When using the following syntax: executeJSCommand("location.href='StatusDetails.php?CHLD=1\&JNum=1024&JTitle=';"); An error occurs displaying: Invalid Escape Sequence ...

Unneeded return is necessary when utilizing recursion and restarting the application

Recently, I successfully recreated the 2048 game in Java, which was a pleasant surprise to me as I didn't expect to create something this "advanced." During the game, when tiles are moved, a new square/tile/number needs to be generated. To find an av ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

Creating solid, dotted, and dashed lines with basic HTML/CSS for tcpdf rendering

Take a look at the image below. It combines two graphs, with different curves represented by solid lines, dotted lines, and various forms of dashed lines with varying stroke lengths. In summary: I am looking for a simple way to create solid lines, dashed ...

Dynamic navigation menu shifting when bolded

Looking at my menu, you'll notice the following layout: If one clicks on Recruitment in the menu, it correctly bolds the text as intended. However, an issue arises where the entire menu unexpectedly shifts 1 or 2px to the left. The menu is implemente ...

Python selenium is now equipped to enable the 'Play DRM' feature

As I dive into learning Python, I decided to take on a project that involves using Selenium to automate interactions with Netflix through Firefox. The challenge I've encountered is the need to enable 'Play DRM' in order to stream content. Un ...

Stopping an endless loop in JavaScript by pressing a key on the keyboard can be a useful

I've been working on creating a JavaScript game and am currently tackling the challenge of implementing gravity. One crucial aspect I need to address is creating an infinite loop without causing the browser to crash. Is there a way for me to include a ...

Is there a way to make my HTML file search beyond its current directory?

My HTML file seems to be having trouble searching in a different folder for the CSS file that I've linked. How can I make it search in the correct folder? I have my HTML file in a directory named INDEX and the CSS file in another directory named STYL ...

What is the method for using jQuery to retrieve hidden fields with the visible attribute set to "false"?

How can I access a control with "visible = false" attribute using jQuery? Some code examples would be appreciated. <tr>   <td class="TDCaption" style="text-align: left">     <asp:Label ID="lblMsg" runat="server" EnableViewState="False" F ...

The Angular material datepicker fails to organize items in a horizontal row

My web application features an Angular Material datepicker, however, I am facing an issue where not all elements are showing up in a row. The view is as follows: Datepicker To prevent any custom CSS from impacting the view, I have removed all customized ...

How to prevent the scroll bar from affecting a fixed div located at the top of the page

Check out this website: I am struggling with setting up a fixed menu div at the top and a content div below it on my site. I want the scroll bar to only apply to the content div and not the header div at the top, but I can't seem to figure out how to ...

My test script consistently runs an unnecessary additional instance when using Selenium Grid

Scenario Overview In my Selenium Grid setup, tests are running in Chrome and Firefox using Gradle. However, there is an unexpected issue where a third test instance runs in the default browser and fails. Expected Test Flow Chrome test passes successful ...