I am encountering issues with my XPath in certain scenarios

When attempting to do an assertion with the total of a dynamic table, I am encountering difficulties writing XPath that works for all scenarios. The issue arises because the table sometimes contains only 1 image and other times it may contain 5 images.

In the provided example, I have successfully used the following XPath: enter image description here WebElement totalElement = driver.findElement(By.xpath("//*[@id='image_table']/tbody/tr[5]/td[4]")); However, this XPath fails when the size of the table changes.

Below is the design of the webpage for reference:

<table class="table table-bordered table-striped table-hover display" id="image_table">
  <tr>
      <td colspan="2" class="text-right">Total</td>
      <td class="text-center">1,650</td>
      <td class="text-center">19,936</td>
      <td class="text-center">21,586</td> (attempting to write xpath for this total)
  </tr>

Answer №1

Consider using CSS for this:

#gallery_table>tr>td:last-of-type

This targets the last item in the table.

Update: Taking into account a comment, a more reliable CSS selector would be:

#gallery_table tr:last-of-type>td:last-of-type

Answer №2

//td[contains(string(),"Total")]/following-sibling::td[last()]

Locate the td containing the word total and then identify the last subsequent td element.

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 use of Kubernetes PersistentVolume and PersistentVolumeClaim may be contributing to issues experienced with my pod, which crashes when attempting to copy logs

I have set up a PersistentVolume with the following configuration: apiVersion: v1 kind: PersistentVolume metadata: name: mypv-shared spec: accessModes: - ReadWriteMany capacity: storage: 5Gi hostPath: path: /data/mypv-shared/ Subseque ...

What is the best way to choose a portion of this text?

Hey there! I'm currently attempting to extract only the [code] section here, but I'm having some trouble. import requests token = 'token' id = "35633560231" headers = { 'Authorization': 'Bearer ' + to ...

Changing the CSS Border of Thumbnails

Take a look at this screenshot of a thumbnail grid. I need to reposition the gray border so that it sits below the price and is consistently aligned across all products. The challenge arises when product titles vary in length, causing the price to be posit ...

Ways to conceal a text-filled div behind an image using 3D transformation

I've been working on creating three images with a fun 'flipcard' effect for the past day and a half, and I believe I'm getting closer to achieving my goal. However, there is one issue remaining, as you can see in the codepen. When the ...

Gorgeous Stew encounters a cautionary message before encountering a glitch in the middle of the

Currently, I am systematically going through every Wikipedia page related to a specific date (from January 1 to December 31). My primary focus is to extract the names of individuals who celebrate their birthday on that particular day. However, as I reach A ...

Changing the WebElement into an integer

Is it feasible to convert a WebElement to an integer? If so, what is the correct approach for doing so? ...

Implementing a Moving Background Image with CSS3 or jQuery

I am facing an issue with a background image that has a file size of 4+ MB in PNG format. My website is a single page website and I want to use this background image, but I'm unsure how to go about it. Is there a way to reduce the size of the image s ...

Do XAML-based apps on Windows 8 provide a noticeable speed advantage over those built with HTML and CSS?

We are in the process of developing a photo-heavy Windows 8 Metro-style app and are concerned about UI performance. While choosing Objective-C over HTML for iOS was an easy decision to achieve the necessary UI performance, we are unsure about the speed com ...

The Super Sub menu is failing to display correctly as intended

I encountered an issue while trying to create a Super sub-menu. The problem is that the super sub-menu appears when I hover over the main menus, instead of the sub-menus. I suspect there may be something wrong with the display: none; attribute, but I' ...

Troubleshooting ASP.NET Content Page Error: jQuery Object Expected

Currently working on designing a personal ASP.NET Web page, I have encountered an issue with making a sticky div using jQuery. Despite all my other jQuery functions functioning properly, the sticky div seems to be causing trouble. stickydiv.js $(document ...

Adding the current date and time to an Excel file

I am encountering a "java.io.FileNotFoundException:" exception when trying to run the code below. My goal is to add the current system date and time to an xlsx file. Can you help me identify the issue? String currentDate = new SimpleDateFormat("MM/dd/yy ...

polygon with five or more sides

Can divs be created with shapes such as five or six corners? I am looking to create clickable zones on a map and any alternative methods would be greatly appreciated. ...

Update the contents of a div element with JavaScript and PHP combo

When I click on the following div, I want to replace it with a similar one from file.php. The div tag in question is: <div style="display: none;" id="extra" class="container2" xml:id="page"> <div class="title"> <h2>Stuff to check out< ...

Unpredictable loading strategy using Selenium Webdriver

I am facing issues when trying to load my page using webdriver. My current workaround involves using the unstable load in Firefox, which is not ideal. I am open to exploring alternative solutions. The Main Issue The root cause of my problem lies in the fa ...

Changing the size of an iframe and closing it by pressing the ESC

I am developing an application that requires the ability to resize an iframe. When the user clicks the "Full Screen" button, the iframe should expand to occupy the entire screen. The iframe should return to its original size when the user presses the "Es ...

Dynamically Growing Navigation Bar Elements with Nested Subcategories Based on Class Identification

Let's say you have a menu bar structured as follows: <nav> <ul class="nav"> <li class="menu1"><a href="#">Menu Item 1</a></li> <li class="menu2"><a href="#">Menu Item 2</a> <ul& ...

The hyperlink to a different webpage does not trigger any JavaScript functionalities or render any CSS styles

I am facing an issue with linking HTML pages that run Javascript and JQuery Mobile from another HTML page. My link setup is as follows: <a href="hours.html">Hours</a> The linking page and the linked pages are in the same directory. However, ...

If a web browser crashes while using Selenium, the process may become unresponsive

As I work with selenium and python, I have been incorporating implicit waits and try/except statements to handle errors. However, a challenge I've faced is that if the browser crashes (for instance, if the user closes it during program execution), my ...

How do I switch the background-image on li hover and revert it back when I move off the li element?

Looking to update the background image of a div upon hovering over a li element? Check out this example here. So far, I've got that part working fine. But now I want the background picture to revert back to its original CSS when I move away from the l ...

Vuejs components that have checkboxes already selected out of the box

Hey there, I'm facing a small issue. Whenever I click on the areas highlighted in the image, the checkbox on the left gets marked as well. Any idea why this might be happening? https://i.stack.imgur.com/nqFip.png <div class="form-check my-5&qu ...