If the elements with the "bg-primary" class are dynamically activated on the site, you might face a timing issue where the elements are unavailable when you try to find them using Selenium. To resolve this, you can use WebDriverWait to wait for the elements to become visible before retrieving their href values. Here's how you can modify your code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for the elements with "bg-primary" class to become visible
elems = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "bg-primary")))
for elem in elems:
if elem.get_attribute("href"):
print(elem.get_attribute("href"))
By using WebDriverWait
with EC.visibility_of_all_elements_located
, the code will wait for up to 10 seconds for the elements with the "bg-primary" class to become visible on the page before attempting to retrieve their href values. This should ensure that you get all the elements you are looking for. If the elements are still not selected, you might need to adjust the waiting time or use different expected conditions based on the behavior of the website.