2

I need to scrape in python a page that has some classes that are number like class1, class2, class3, ... classN and I would like to locate the text contained in all those classes.

I tried using find_element() function:


list = []
for i in range(N-1)
  list.append(driver.find_element(By.CLASS_NAME, "class" + str(i+1) ).text))

but unfortunately I get this error message:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".class1"}
  (Session info: chrome=107.0.5304.68)

Is there any other way I could succeed in this scraping?


EDIT

This is the page I would like to scrape and I would like to get the games won by both players per every set.

In the source code the classes are named "smh__part smh__home smh__part--" + str(i+1) and respectively "away"

5
  • 1
    You need to post the relevant html? but unfortunately this did not work what does this mean, are you getting syntax error or what's the error? Commented Oct 31, 2022 at 14:11
  • I just edited the question with the error log message. I do not think there is anything more that could help in html code I need to scrape, however, if it may help you I'll post the source code Commented Oct 31, 2022 at 14:14
  • Please check if element present in iframe or it is opening in new window? without steps to reproduce the issue, it's hard to provide a solution. Commented Oct 31, 2022 at 14:22
  • Are you sure those elements having single class value? can you share at least HTML of some of those elements? Preferably the link to that page itself... Commented Oct 31, 2022 at 14:31
  • Edited the question with the link. Thank you in advance! Commented Oct 31, 2022 at 14:34

1 Answer 1

2

Class_name only accept single class name not multiple. Instead use css selector. use python format() function

list = []
for i in range(N-1):
  list.append(driver.find_element(By.CSS_SELECTOR, "[class='smh__part  smh__home smh__part--{}']".format(i+1)).text)

OR CLASS_NAME with single class value

list = []
for i in range(N-1):
  list.append(driver.find_element(By.CLASS_NAME, "smh__part--" + str(i+1) ).text)
Sign up to request clarification or add additional context in comments.

2 Comments

This was simply great. Thank you very much! Can I ask you where could I find a documentation for handling strings with the format function?
It is python function format w3schools.com/python/ref_string_format.asp

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.