from selenium import webdriver
from selenium.webdriver.common.by import By
#Keep Chrome browser open after progarm finishes
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach",True)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.python.org")
events_times = driver.find_elements(By.CSS_SELECTOR,value=".event_widget time")
events_names = driver.find_elements(By.CSS_SELECTOR,value=".event_widget li a")
for name in events_names:
print(name.text)
driver.quit()
I have this simple program that uses Selenium webdriver on the python main page, but i don't understand why it doesn't print the events, because I added the elements correctly.
In your code, find_elements
is not finding any elements with that CSS locator.
Since you haven't clearly mentioned which elements are you trying to locate and print. Am assuming it is below:
If that is true, refer the code below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.python.org")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
events_times = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//h2[text()='Upcoming Events']//following::ul[1]/li//time")))
events_names = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//h2[text()='Upcoming Events']//following::ul[1]/li//a")))
for time, name in zip(events_times, events_names):
print(f"{time.text} - {name.text}")
driver.quit()
Console result:
2024-10-31 - PyCon FR 2024
2024-10-31 - PyCon Zimbabwe
2024-11-13 - Python Ireland Monthly Meetup
2024-11-14 - PyCon Sweden 2024
2024-11-16 - PyCon Hong Kong 2024
Process finished with exit code 0