Difficulty with locating elements that are not easily identifiable using standard methods like xpath, id, or class

Identify the 3rd td in tr with a class that includes "zebra"

The image displays my current task. I am required to pick out the quantity corresponding to each of the 4 animals, which is always found in the 3rd td of the tr element. In the provided html, you can observe that zebra has a quantity of 1 and lion also has a quantity of 1.

My attempts at locating the item by its class have been unsuccessful so far. Could it be due to the presence of spaces within the text?

A straightforward xpath query won't suffice as the tr tags are dynamic based on user inputs from the previous page.

I have also made an effort to select via xpath using the contains method without success. Perhaps I am not executing it correctly.

Answer №1

A common mistake when attempting to locate an element with a class name containing a space is using the className locator, which will not work in this case. Instead, you should opt for CSS selector or XPath.

    //CSS selector
    driver.findElement(By.cssSelector(".line_item.Zebra"));
   //XPath can also be used 
    driver.findElements(By.xpath("//*[@class='line_item Zebra']"));

Answer №2

line_item Tiger and Wildcat belong to the same category as WebElement. You can locate them using className.

driver.findElement(By.className("Tiger"));

If you wish to see all the feline creatures, utilize the line_item tag. This will display all the felines.

List<WebElement> felines = driver.findElements(By.className("line_item"));

felines now contains a list of four elements with the class line_item. To access the third <td> in each one, you can iterate through the WebElements in the list.

for (WebElement feline : felines) {
    List<WebElement> tds = feline.findElements(By.tagNmae("td")); // all the <td> tags in that feline
    String quantity = tds.get(2).getText(); // get the text of the third <td>
}

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

Tips for choosing an element created by JavaScript code

I need help selecting the Wireless button on a pop-up window that appears within the same window. There are no iFrames generated and I'm unable to locate any elements inside this Wifi-Connect window. Any suggestions? Here is the code I've attem ...

Extract the title of a YouTube video and convert it into a string using JSONObject

I'm struggling to figure out how to extract a JSONObject from a method and convert it into a String in order to retrieve the titles of YouTube videos. public static String getTitleQuietly(String youtubeUrl) { try { if (youtubeUrl != null) ...

What is causing the delay in retrieving elements using getX() method in Selenium?

Recently, I've been experimenting with web scraping using Selenium. However, I've noticed that there is a delay when calling getText(), getAttribute(), or getTagName() on the WebElements stored in an ArrayList from the website. The website I&apo ...

Selenium works well with transactionless database cleaning in Rails/RSpec/Capybara, but Webkit does not support it

After configuring my RSpec environment to use a truncation cleaning strategy for Capybara tests, I encountered an issue with Webkit as the Javascript driver. Surprisingly, when using Selenium, everything works smoothly. The relevant RSpec configuration wi ...

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 ...

Validating phone numbers with regular expressions in Java

I recently started learning Java and Regex, and I'm currently facing a challenge with phone number validations. The task at hand is to create simple phone number validations. I already have an input field in my HTML code that has the attribute type=" ...

Discovering the parent element of a find_element_by_partial_link_text: a step-by-step guide

Using the find_element_by_partial_link_text selector to locate the "next" button for crawling continuity has proven problematic for me. The presence of the word "next" in various other links on the page often disrupts the script execution. Despite attemp ...

When the browser is not in the foreground, clicking on the Bootstrap datepicker with Selenium does not register

When you click on the input field <input id="dp1" class="span2" type="text" value="02-16-2012"> If the browser is in the background, the datepicker popup will not display. Even using javascript or jquery to click the input field does not show the ...

Generating a tree structure using a JavaScript array

Looking to build a tree structure from a given list of data where the paths are represented like: A-->B-->C-->D-->E.. A-->B-->C-->D-->F.. A-->F-->C-->D-->E.. . . . All possible data paths are stored in an array. The de ...

Locate an original identifier using Selenium WebDriver

In search of a distinctive identifier for the "update profile picture button" on my Facebook account to utilize it in automation testing with Selenium WebDriver and Java. I attempted driver.findElement(By.className("_156p")).click();, but unfortunately, i ...

When Selenium webdriver is deployed onto TFS, it may return an empty string for the breadcrumb text from an IWebElement

Welcome to my first post on StackOverflow! I've provided a detailed description of the issue, and I'm open to providing more information if needed. Issue Overview: I am currently using a standard breadcrumb for navigation on my website, with the ...

Automating Checkbox Selections with Selenium in Python

I'm having trouble clicking on a checkbox. Here is the HTML Code: <div class="mb-1 p-3 termsCheck"> <input class="form-check-input float-end" type="checkbox" value="" id="flexCheckDefau ...

Looking for a website to conduct load testing? You'll need to log in first in order to access

I am in the process of developing an Automated Testing Python Project to conduct comprehensive testing on a website. This project involves executing Functionality, Accessibility, and Load Testing on the website, with detailed reporting of the outcomes. The ...

Tips for launching and controlling new tabs using selenium

How can I open a new tab with the 'https://www.gmail.com' url, extract some information, and then return to the original page using Python 3.8.5? I am currently opening the new tab with CTRL + t command, but I'm unsure how to switch between ...

The && symbol in JSONPath: combining conditions efficiently

Here is an example of JSON data taken from the tutorial: { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", " ...

Selenium in chrome does not support automatic translation

Whenever I use Selenium WebDriver in Chrome to open a webpage in a foreign language, it doesn't translate the page to English automatically. Is there a way to change this behavior? Interestingly, when I manually open the same page, it does get tran ...

Improving the Appearance of JSON Data on Android: Step-by-Step Guide

My Android application displays raw JSON data from an API, but I want to format it in a pretty print style like this: https://i.stack.imgur.com/hJSe3.png Instead of the current display which looks like this: https://i.stack.imgur.com/kLH9l.png Here is ...

What is the best way to split a string based on multiple delimiters?

Can someone help me extract the text 18-Aug-2019 14:00 from Egypt Today Last Update Time: 18-Aug-2019 14:00 (GMT)? I tried splitting at ":" as my first step, followed by splitting at " (" (essentially 2 splits), but that approach is not working. Is there a ...

The hover functionality is not functioning as expected on the demo website when using Selenium WebDriver

I have attempted to use the following code. public class LocateMultipleItems { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); dr ...

Export Page Elements from Selenium to a File

I've created a script using Selenium IDE for Firefox on Windows 7 with FF 25.01 IDE version 2.4.0. The script is functioning well, but I'm interested in saving a particular page element from the query it executes to a text file. The page being l ...