I wanted to get elment by html selector a lit of events from python.org
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_driver_path = "C:\development\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("https://python.org")
event_time = driver.find_element(By.CLASS_NAME, ".event-widget time")
for time in event_time:
print(time.text)
CodePudding user response:
About Invalid Selector Exception:
Selenium throws an InvalidSelectorException when an XPath or CSS selector does not conform to the XPath or CSS specification. In other words, an InvalidSelectorException occurs when you pass in a selector which can’t be parsed by Selenium WebDriver’s selector engine. This occurs when an element retrieval command is used with an unknown web element selector strategy
Problem:
In your case, I do not see any class attribute with the name .event-widget time in the entire HTML structure.
Solution:
Try using some other locators like ID, name or XPath. If you are specific about using ClassName, then make sure that web element's class attribute is accurately matching in your code.
CodePudding user response:
The locator type is wrong, it should be CSS_SELECTOR
not CLASS_NAME
, also you have to mention find_elements
not find_element
:
event_time = driver.find_elements(By.CSS_SELECTOR, ".event-widget time")
for time in event_time:
print(time.text)
Output:
2023-02-05
2023-02-16
2023-02-21
2023-02-25
2023-03-06