Leveraging the power of selenium's CSS selector for various elements

In my Perl script, I have multiple lists of links that can collapse and expand. To determine the total number of links in a list, I am using the get_xpath_count('//li/a') function.

The challenge I am facing is how to extract the actual names of these links. I have attempted to use xpath with limited success and decided to explore the option of CSS selectors. My attempt at using

get_text('css=li a:nth-child('.$i.')')
resulted in displaying a [-] icon next to the top link followed by an out-of-range error. Since I lack familiarity with CSS selectors, I am hoping someone can provide guidance on how to achieve this. Feel free to ask for any additional information if needed.

Answer №1

Here's a suggestion (in pseudo-code, because I steer clear of Perl):

list linkNames;
count = selenium.get_xpath_count('//li/a');
for (i = 1; i <= count; i++) {
   list.append(selenium.get_text('xpath=(//li/a)[' + i +']');
}

Keep in mind:

  • XPath expressions start counting from 1 to n, unlike the usual 0 to n-1 in C-derived languages.
  • The correct XPath format for selecting the i'th match of a pattern is (pattern)[i], not pattern[i].
  • Selenium doesn't automatically assume the (pattern)[i] locator is an XPath, so you must specify it by starting with xpath=.

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

What is the best way to find an element using either 'className1' or 'className2'?

Within my project, all locators are defined as elements of an enum and follow this format: For example, if it is a CSS locator - DIV_LOCATION("css=div.location-text.text-overflow"). This method parses the string and identifies it as a CSS locator if it be ...

My project is experiencing issues with Font Awesome and Sass

Currently I am working on Windows and utilizing bower for my project. Below is an excerpt from my app.sass file: $fa-font-path: "/lib/font-awesome/fonts" @import 'lib/font-awesome/scss/font-awesome' The project runs smoothly on Linux but encoun ...

"Utilize jQuery's append method to dynamically add elements to the DOM and ensure

I am currently facing an issue with my AJAX function that appends HTML to a page using the jQuery append method. The HTML being appended is quite large and contains cells with colors set by CSS within the same document. The problem I'm encountering i ...

Enhance your theme property in Styled Components by incorporating numerous style properties

Currently, I am delving into the world of styled components for a fresh project and exploring the theming capabilities it offers. I am finding it challenging to determine the best practice for adding multiple style properties to a single object, essentiall ...

Using Python to control two separate Chrome instances via webdriver

Preparing for my upcoming Python and Selenium Webdriver test, I have created my first file called login with a class (login.py): from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) class L ...

Looking to streamline your design? Find out how to translate multiple elements to the same position seamlessly

Greetings! This is my debut post on this platform. I am currently engrossed in a project that has presented me with a challenge, and I'm reaching out to seek your insights on a potential solution. Here's the scenario: I have a parent div containi ...

Text in SVG file misaligned at the edge

After creating an SVG with a base64 background image and two text areas for top and bottom texts, I encountered an issue on Internet Explorer and Edge. The problem is that the bottom text is aligned to the left instead of the center, and its position is in ...

Gather updated information from a hover popup using Selenium and Python for a fresh data table integration

A few years ago, I successfully managed to scrape hover elements using Selenium. It was a bit challenging back then to select the correct hover table element that only appeared on hover. Recently, the website underwent a complete style overhaul, which seem ...

Version 5.1.3 of Bootstrap now combines the $colors with the $theme-colors map for enhanced customization

I am attempting to introduce some color variables like "blue", "indigo" ... to the $theme-colors map. It appears to be functioning, but after compiling, it seems duplicated in the :root selector as shown in the image below. Here's ...

Unable to view image when using material-ui CardMedia component

Hello, I've encountered a problem with rendering an image in my application. Let me explain the issue in a simplified manner. So, I have a card component (MyCardComponent) where I need to pass a string prop containing the image file location. The goa ...

A guide to aligning text on the right side of an image with HTML and CSS

Looking for assistance to align text on the right side of an image at the same level. Below is the code I have: <a class="following-row" href="index-2.html?pid=2306"> <img alt="Girls logo" src="photos/users/35257/resized/9fd79 ...

Exploring RemoteWebDriver in debugging headless docker containers

I am currently facing an issue with running a RemoteWebDriver through a docker instance. The browser doesn't open, making it difficult for me to debug the code on my machine. Below is the code snippet responsible for creating the driver, but it seems ...

Can Selenium 3 detect HTML5 pseudo-classes such as :invalid or :valid?

Take a look at this basic HTML5 form example: If you click Submit, running driver.findElement(By.id("firstName")).getAttribute("class") would not result in ":invalid" being returned. Is achieving this possible without the use of JavaS ...

Creating a collapsible accordion feature with jQuery using a specific HTML layout: wanna learn how?

I am looking to create an accordion effect with the structure provided below. The goal is to have the corresponding article toggle when clicking on .booklist>li>a, allowing only one article to be open at a time. Can someone assist me with writing thi ...

Add a layer on top of the background in order to cover the entire screen

After researching various SO questions and answers, as well as examining different JSFiddle and Codepen examples, I have yet to find the solution I am looking for... Here is a sample fiddle of my current issue: https://jsfiddle.net/ts4t13hn/3/ Below is ...

Guide to effectively utilizing the Reports public class for logging functions in a project, allowing seamless access to its public static methods from other classes within the same project

Below is a sneak peek into my code: package something.util; public class Reports { public static void logStatus(LogStatus testStatus, String testDetails) { test.log(testStatus, testDetails); } } package something.pages; public class MainP ...

What is the best way to place a button within a line of text

I am trying to achieve a layout similar to this: -------------------button------------------ I can add text inside the line, but it doesn't look good when I add a button. Any suggestions on how I can improve this? ...

Adjusting Padding for Improved Mobile Display of BS4 Grid Cards

I am working on a bootstrap-4 grid card layout for my page. On mobile devices in portrait view, I want the cards to be displayed 2 up. However, I am facing an issue where there is extra padding between the cards compared to the padding on the outside edges ...

What are some creative ways to conceal text using CSS or JavaScript?

Looking for a solution: <div id="body" role="main"> <h1>Title</h1> <p>My site is mysite.com</p></div> I always have <div id="body" role="main">, <h1>, and <p> tags on each page. Is there a way to ...

Encountered an issue with chromium-browser: [670] Unable to activate Xlib threaded mode

Initially, my goal was to execute a headless selenium webdriver on a Raspberry Pi 3 running Raspbian. After multiple failed attempts, I decided to simplify and focus solely on running chromium-browser which is required for the webdriver. Upon execution, I ...