1

I am having problems getting data from a element using Selenium with the line:

bets = driver.find_elements_by_class_name('sgl-ParticipantFixtureLink gl-Market_General-cn1 ')

I can get the data by using XPath, but getting all the values since they have the same class name would be more efficient.

Since I am pretty new to HTML, Python, JavaScript etc, I was wondering if need to change the class name, using:

"_" or "-"

to fill up the blank spaces in the class name?

bets = driver.find_elements_by_class_name('sgl-ParticipantFixtureLink-gl-Market_General-cn1-')

or

bets = driver.find_elements_by_class_name('sgl-ParticipantFixtureLink_gl-Market_General-cn1_')

It does not seem to return any value, but neither does it return null.

3
  • Can you please explain what you mean by this - "I can get the data by using XPath, but getting all the values since they have the same class name would be more efficient." Commented Jun 15, 2021 at 19:36
  • Since you don't seem to be aware, when an element has spaces in the class attribute, that means it has multiple classes. Commented Jun 15, 2021 at 19:40
  • Using XPath I only get 1 value. But if I was to get all the values with 1 function that would be more efficient, wouldn't it? Because all the values I want to get has the same "class name". @itronic1990 Commented Jun 15, 2021 at 19:46

2 Answers 2

1

When an element has spaces in the class attribute, that means it has multiple classes, so you'll need to handle that another way.

One option is with CSS selectors.

https://selenium-python.readthedocs.io/locating-elements.html#locating-elements

https://selenium-python.readthedocs.io/api.html#locate-elements-by

bets = driver.find_elements_by_css_selector('.sgl-ParticipantFixtureLink.gl-Market_General-cn1')

Start with a period (.) and have a period between each subsequent class (nothing at the end)

This will return a list containing all in elements with both of those classes

For example, if you wanted to convert this to a list of text, you would do this:

# after the code above
texts = [bet.text for bet in bets]
Sign up to request clarification or add additional context in comments.

1 Comment

Now I can find all the elements, but can't get the value from them which is a number, between the "::before" and "::after". I tried using .text but get AttributeError: 'list' object has no attribute 'text'.
0

As mentioned by Samathingamajig in case of multiple class names the simplest way to locate the element(s) is to use css_selector.
If there are several elements matching that locator you can iterate through the list of elements and get text from each one of them. Like this:

bets = driver.find_elements_by_css_selector('.sgl-ParticipantFixtureLink.gl-Market_General-cn1')
for bet in bets:
    text = bet.text
    print(text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.