Having difficulty locating the xpath list, attempting to utilize a wildcard to search for text or style

My task is to locate the XPath for the section under “Main Lists” on this specific site. This is what I have so far:

//div[starts-with(@class, ('sm-CouponLink_Label'))]

However, this search yields 32 results...

`//div[starts-with(@class, ('sm-CouponLink_Label'))]`[contains(text(),'*')or[contains(Style(),'*')] 

Unfortunately, in this particular case, I need to stick with XPaths and not CSS.

The website in question can be found here, my relevant code is available, and here's an accompanying image of the desired XPATH.

I've also experimented with the following:

CSS:

div:nth-child(1) > .sm-MarketContainer_NumColumns3 > div > div

Xpath equiv...:

//div[1]//div[starts-with(@class, ('sm-MarketContainer_NumColumns3'))]//div//div

But it doesn't seem to be effective.

UPDATED WORKING CSS:

div.sm-Market:has(div >div:contains('Main Lists'))   * > .sm-CouponLink_Label

Xpath:

//div[Contains(@class, ('sm-Market'))]//preceding::('Main Lists')//div[Contains(@class, ('sm-CouponLink_Label'))]

Still not functioning correctly..

I'm uncertain if Selenium has a comparable feature to :has

Alternatively...

Something along the lines of:

//div[contains(text(),"Main Lists")]//following::div[contains(@class,"sm-Market")]//div[contains(@class,"sm-CouponLink_Label")]//preceding::div[contains(@class,"sm-Market_HeaderOpen ")]

(wrong area)

Answer №1

If you need to extract specific elements, try using the code snippet below:

league_names = [name for name in driver.find_elements_by_xpath('//div[contains(@class, "sm-Market") and .//div[contains(text(), "Main Lists")]//div[contains(@class, "sm-CouponLink_Label")]') if name.text]

This will provide a list of nodes that are not empty.

Answer №2

It seems like you are looking to refine your initial XPath query to specifically target div elements that either contain text or have a style attribute. To achieve this, the following XPath can be used:

//div[starts-with(@class, ('sm-CouponLink_Label'))][@style or text()]

LATEST UPDATE

After receiving more information from you, it appears that you are now interested in selecting div elements with the 'sm-CouponLink_Label' class within the 'Main Lists' section. To accomplish this, you can integrate 'Main Lists' into the XPath expression. Here is one possible approach (formatted for better readability):

//div[
    div/div/text()='Main Lists'
]//div[
    starts-with(@class, 'sm-CouponLink_Label')
        and
    normalize-space()
]

By using normalize-space(), any empty div elements will be excluded from the results. This should yield the expected 5 elements. I tested this in Chrome and obtained the following result:

https://i.sstatic.net/GvlCz.png

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

It appears that the Selenium driver is having difficulty retrieving the HTML content from the specified URL

Apologies for the ambiguous title of this question, I struggled to find a way to provide more detail. Here is the issue at hand. EDIT: The command browser.get(url) appears to be unresponsive. Here is the current environment I am working on (output of uname ...

Personalize Material design slider Label - final element

When using Material-ui, the default position of Slider's labels is centered: However, I require the labels to have a space-between position. For example: To achieve this, I tried using the '&:last-child' property for the markLabel clas ...

Ways to expand a navbar background without compromising the central alignment of its elements

I need the blue color of the navigation bar to cover the entire screen, while keeping the About, Updates, and Who am I? links centered. The background should adjust accordingly if there are any resizing changes. If there's a better method for centerin ...

Position the image, text, and header within a box in uniform alignment

Currently, I am working on a design that includes an image, a header, and some text inside a box. However, I am facing an issue with aligning the header so that it appears above the rest of the text. The layout is not turning out as expected. https://i.ss ...

Python3 Method to Eliminate Line Breaks

Looking for a way to eliminate Vertical Tab and Back Space (Character ASCII code 013 010) with python? I've attempted the following codes without success. text = re.sub('\n', ' ', text) // executed, removing all text except l ...

What is the reason for receiving the error message stating "list object lacks attributes *()" rather than "list object does not have methods *()"?

I have a question regarding calling methods and attributes in Python. It's often mentioned that when you call a method which does not belong to an object, you'll receive an error stating "list object does not have that attribute." My confusion li ...

Outline of a Bootstrap row

I am trying to create a row layout where the background extends beyond the designated row area. The desired design is shown in the image, with an additional white space before the actual text of the row. However, when I attempt to start a new row and set ...

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

What is the best method for inserting a URL link using jQuery within a CSS element?

Check out this code snippet: <span class='maincaptionsmall'> Test </span> Due to certain ajax and jquery scripts on my page, I am unable to use HREF as a link. In other words, the following code won't work: <span class=&ap ...

Ensuring the input field and button are positioned in a horizontal line

Currently, I am developing a to-do list application using React and Chakra UI. I am trying to align the input field and the button on the same line for a more organized layout. This is the current layout: image I aim to achieve a design similar to this: ...

Combining a Hyperlink with a Table Target

I'm currently trying to find a way to target both a table cell (TR > TD) and a hyperlink within the same row. Here's an example of the HTML: <div class="CourseLayout"> <table width="100%" border="0" cellpadding="0" cellspacing="0"&g ...

Troubleshooting a CSS Problem with Safari

I am experiencing an issue where a box with a hover overlay does not display correctly on Safari browsers (desktop and mobile). The styles such as width, height, and padding are not applied properly initially. Oddly enough, when I toggle these styles off a ...

What is the best way to format these div elements in order to create a functional comment section?

For the past few hours, I've been struggling to figure out what’s causing issues with the styling on my webpage. The layout should display a user post on one side and a comment-section along with the comment form on the other. Within the PostComment ...

Removing a background by linking a CSS file

Issue Resolved: I have found the solution. The problem was that the css document still contained html tags. Thank you to everyone who provided assistance. While designing a website using css, I encountered a situation where the site worked perfectly when ...

What could be causing my function to execute thrice?

I cannot seem to figure out why my tag cloud is causing the required function to run multiple times instead of just once when I click on a tag. This issue persists whether using jQuery or plain JavaScript. What am I missing? The code I have is very simple, ...

Event listener added for a specific class, with certain individuals within the class not impacted

I've created a grid to serve as a menu, with a top row consisting of an unordered list (ul) containing three list items (li). Each of these li elements contains another ul with four li items inside. I've added an event listener that triggers when ...

JavaScript fade effects toggle

Do you have a question? In Vue.js, achieving the fade in / fade out effect can be easily done by specifying it through the component. new Vue({ el: '#demo', data: { show: true } }) .fade-enter-active, .fade-leave-active { ...

Utilizing pandas and selenium to retrieve data from a table

import pandas as pd import os from webdriver_manager.chrome import ChromeDriverManager import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver. ...

The clash between React Inline styles and Client CSS can lead to conflicts in styling

Our application needs to be loaded into the client's page, which is built using React. We are implementing React Inline Styles by defining styles as objects. However, we have encountered an issue where any CSS specified by the client using tag attribu ...

While using Nav, it is necessary to adjust the height of the drop-down menu to accommodate the content when the sub-menu

My dropdown menu has multiple submenus, and I'm facing an issue where the submenu content overlaps the container when opened. I want the container to scale to the height of the submenu content when opening, and then reduce back when closing the submen ...