Issue
This is my first time using Selenium. I've looked at other questions in StackOverflow before, but they haven't been very helpful to me. I would like to print "only" the text Giornata 38 and Giornata 37 from the website https://link. No the content, but only these two texts for illustrative purposes. I specify that everything is for educational, study and formative purposes only for myself that I am learning Python.
What am I doing wrong? Python code or html inspection on the site? IMPORTANT: If I have written the code wrong and you modify / improve it, remember to stay Firefox, do not replace it with Chrome. Thank you
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('link')
giornata = browser.find_element_by_class_name("event__round event__round--static")
print(giornata)
browser.quit()
display.stop()
Error: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .event__round event__round--static
Solution
The class event__round event__round--static
contains a space " "
, you have to remove this space and connect the class with a dot .
.
Instead of:
giornata = browser.find_element_by_class_name("event__round event__round--static")
Try:
giornata = browser.find_element_by_class_name("event__round.event__round--static")
print(giornata.text)
Based on your comment, if you want to access specific elements, such as "Giornata 37" and "Giornata 38", you can use the :nth-of-type(n)
CSS selector, div.event__round:nth-of-type(2)
will select a div
and then the second class event__round
.
In your example:
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(15)").text)
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(2)").text)
Output:
Giornata 37
Giornata 38
Answered By - MendelG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.