Accessing an object from a webpage using Selenium: A step-by-step guide

Today marks my debut using Selenium IDE, and I must admit that my knowledge of web development is limited. My team is currently working on a website, and as part of my task, I need to write tests for it. The website features several charts showcasing data we've collected. My goal is to search for specific words within each chart to determine if they are accurate and identify any discrepancies.

However, I'm facing a challenge in accessing each "chart object" individually. When I use the command:

verifyTextPresent, target =, value = Good value

The command works fine (appears green in Selenium IDE) when one of the six charts displays "Good value" correctly. I believe it searches the entire webpage, which explains why it's successful. But when I attempt to test each chart separately, I encounter difficulties.

Here are some strategies I've experimented with:

1) I tried right-clicking on the chart object, and Selenium IDE identified the target as:

css=svg > rect

Despite this, the operation consistently fails, with the log showing:

[error] Element css=svg > rect not found

2) I visited http://docs.seleniumhq.org/docs/02_selenium_ide.jsp#locating-elements to gather insights. Unfortunately, the examples provided didn't align entirely with our setup, so my attempts were somewhat random and ineffective. While I'll include the source HTML below for context, here are some other methods I tried:

id=numRecs
identifier=charts-6
css=class#charts-container//chart-6

A thorough search indicates that these values are unique, reducing the potential for parsing errors.

Below is an excerpt from Chrome displaying partial source for the chart object. More information can be supplied upon request, but these snippets appear crucial – hovering over these div elements highlights the corresponding chart:

<body>
blah blah
   <div id="numRecs" data-chart="3">
      <div class="charts-container" id="charts-6" style="position: relative; overflow: hidden; width: 1267px; height: 300px; text-align: left; line-height: normal; z-index: 0; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif; font-size: 12px;">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1267" height="300">
      blah blah
   <div id="another chart" data-chart="4">
      blah blah
   </div>
   <div id="misc chart" data-chart="5">
      blah blah
   </div>
</body>

I am hopeful someone can steer me in the right direction. Thank you in advance for your assistance.

Answer №1

If you're looking to locate elements, you have the option of using xpath or css. Personally, I rely on Selenium within Java (specifically in Eclipse) with JUnit. Below is a snippet demonstrating how to find elements using xpath:

@Test
public void test1() throws Exception {
    WebDriver driver = new FireFoxDriver();
    String url = "example.html";
    driver.get(url);
    Thread.sleep(5000);

    WebElement anotherChart = driver.findElement(By.id("another chart")); // locating element by id
    String anotherChartText = anotherChart.getText(); // Utilizing getText to verify data ("blah blah")

    WebElement miscChart = driver.findElement(By.cssSelector("#another chart")); // locating element with css
    String attribute = miscChart.getAttribute("data-chart"); // Retrieving a specific attribute of the WebElement ("5")

    WebElement chart6 = driver.findElement(By.xpath("//div[@id='charts-6']")); // locating element with xpath

    WebElement numRec = driver.findElement(By.xpath("//div[@id='numRecs']")); // locating element with xpath
    WebElement numRecChart6 = numRec.findElement(By.xpath("div[@id='charts-6']")); // Using WeElement.findElement() method to locate an object within the WebElement

    driver.close();
}

In my opinion, both xpath and css can be valuable tools for your needs. If you want to validate your code, consider utilizing xpath testers like:

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

Looking for assistance with CSS selectors for the following structure

I am currently working on locating the anchor element "a" that is nested inside a table with the class name table.ic-table-creditReportProduct table tr. I have attempted to find it, but my attempts have been unsuccessful so far. Any suggestions on where th ...

Can the hexadecimal value from an input type color be extracted and used to populate form fields that will then be displayed in a table after submission?

Hello everyone, I'm new to this platform and seeking guidance on how to improve my post! I recently created a CRUD app using Angular. It consists of a basic form with 4 fields, a color picker using input type='color', and a submit button. U ...

Do you know of a more streamlined approach to formatting this SCSS code?

Currently working on a Saleor Site and diving into the world of Django and SASS for the first time. While crafting my own styling rules in SCSS files, I've noticed some repetitive code that could potentially be streamlined. It seems like there should ...

HTML script tag failing to load

As a beginner in using scripts in HTML, I'm currently following a tutorial that suggests loading the script after the body for certain reasons. In my own code, I found that I need to place the script both in the head section and after the body. Remov ...

Are personalized URLs necessary for jump anchor links?

When utilizing 'Jump to' or 'Anchor' links to navigate to a different section of a webpage, should I include a canonical URL or use a 'no-follow' link for SEO optimization? Is it significant at all? And if so, which approach i ...

Different approach to handling overflow without using the hidden property in CSS

Having trouble with my CSS layout I've created a form within a fixed 500 pixel width div that is centered on the page using margin auto; To create a table-like structure within the div, I used div elements as rows. Due to varying heights of these ro ...

BeautifulSoup and Selenium are unable to fetch the complete HTML content from the website

I'm attempting to gather information from the following website: . Specifically, I am interested in extracting box score data such as the Oakland A's total batting average, at bats, and more. However, when I retrieve and view the HTML code from t ...

"Introducing a smooth animation effect to shift text alignment to the center

I'm currently using the code below, but I am not satisfied with the margin adjustment achieved by text-align:center;. Is there a way to smoothly transition the text alignment to center similar to how it is done in the following snippet of code? if ($ ...

Exploring reviews with an innovative combination of Node.js and MongoDB

Recently diving into the world of Nodejs and mongoDB, I am excited to create two distinctive pages: one for users to update their account details such as profile picture, email, and name - all of which would be saved into the database; and another for re ...

How come modifying the css of one component impacts all the other components?

Issue: I am struggling to make CSS changes only affect the specific component I want, without impacting other elements on the page. My goal is to style my dropdown menu without affecting other text input fields: https://i.sstatic.net/Caidv.png Background ...

Determine if a link can be activated or disabled

Controlling the clickability of a link and displaying an error based on the result of an ajax call is my current objective. <a class="lnkCustomer" href="http://localhost/viewcustomer" target="_blank" data-customerno="2 ...

How come the `table-fixed` class in Bootstrap isn't converting my table into a scrolling one?

I am having some trouble with making my table scrollable. I have around 550 rows in the table and I am using the `table-fixed` style from bootstrap, but it doesn't seem to be working as expected. Additionally, the `table-condensed` class is not regist ...

Having trouble pinpointing the elusive element in Selenium?

<div style="overflow:hidden;height:20px;" valign="middle"> <div id="IconImg_iconMenu_icon__dhtmlLib_274" class="imo" style="width:16px;height:16px;background- image:url('images/main/galleries/icon16x16gallery1b.png');background- positi ...

Selenium: The pitfall of using the "if" statement to determine the presence of an element in automation testing

Apologies if this question seems redundant or silly. I am struggling with determining the existence of an element. When running the code below, errors such as "Could not locate the id 'smc'" will occur. if driver.find_element_by_id("smc"): ...

What is the best way to keep an image fixed at the bottom, but only when it's out of view in the section

There are two buttons (images with anchors) on the page: "Download from Google Play" and "Download from App Store". The request is to have them stick to the bottom, but once the footer is reached they should return to their original position. Here are two ...

Trying out the combination of PyInstaller, Selenium, and Pandas

I'm working on a project to develop an application that will search for items on a specified website. My goal is to make this application user-friendly for those who are not familiar with Python. I tried using PyInstaller with the code below, which ex ...

Why is the parameter value becoming null during an Ajax call?

I am passing a parameter from HTML to JSP, but when I try to retrieve the value in JSP, it returns null. Can someone help me figure out what's wrong with my code and provide any suggestions? The value is successfully displayed in an alert using JavaS ...

Utilizing Angular to apply multiple ng-repeat directives with multiple filters

I am working on a project that involves multiple ng-repeat lists with several filters. Currently, I am using (ex:A.value) if (ex:B.value), but I would like to implement multiple filters. The filters I want to incorporate are recommend_search, skill_searc ...

Tips for arranging cards in columns without causing vertical scrolling to be triggered

I am working with Bootstrap 5 and have created a column with multiple cards: https://jsfiddle.net/fzxc1v69/ Currently, all the cards are displayed one below the other, but I want them to be lined up horizontally instead. In my code, the .section class i ...

Is the CSS scale activated by mouseover or click?

My CSS code successfully scales images, but the issue is that it affects every image on the page. I am looking for a solution to apply this CSS only when the user hovers over or clicks on an image. The challenge is that images are added by non-technical w ...