2

I am trying to get to the texts inside the span tags by iterating through the li list of this HTML:

<ol class="KambiBC-event-result__score-list">
    <li class="KambiBC-event-result__match">
        <span class="KambiBC-event-result__points">1</span>
        <span class="KambiBC-event-result__points">1</span>
    </li>
</ol>

but i am getting the error

AttributeError: 'list' object has no attribute 'find_element_by_class_name'

on my code:

meci = driver.find_elements_by_class_name('KambiBC-event-result__match')

for items in meci:

 scor = meci.find_element_by_class_name('KambiBC-event-result__points')


 print (scor.text)

2 Answers 2

5

You are not using items inside the loop. You loop should be

meci = driver.find_elements_by_class_name('KambiBC-event-result__match')

for items in meci:
    scor = items.find_element_by_class_name('KambiBC-event-result__points')
    print(scor.text)

meci.find_element_by_class_name should be items.find_element_by_class_name

Update for Selenium 4

As mentioned in this Stack Overflow post, the find_element_by_* methods are deprecated from Selenium 4 onwards. Therefore, the solution would need to be modified as follows:

from selenium.webdriver.common.by import By

meci = driver.find_elements(By.CLASS_NAME, 'KambiBC-event-result__match')

for items in meci:
    scor = items.find_element(By.CLASS_NAME, 'KambiBC-event-result__points')
    print(scor.text)
Sign up to request clarification or add additional context in comments.

1 Comment

Well, do i fell stupid now, also how do i access only the second child?
0

To answer your second comment, all you need to do is add ":nth-child(2)" to the end of the class name.

The class 'KambiBC-event-result__points' would read 'KambiBC-event-result__points:nth-child(2)' to only access the second child.

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.