To create a loop to click all the links one by one on the webpage, you can modify your current approach to loop through a range of nth-child selectors. I'll provide you with a general Python code using Selenium WebDriver that should help you achieve this task. Note that you'll need to handle exceptions and possibly waiting for elements to be clickable or visible.
Here is a simplified example of how you might structure your loop:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Initialize the WebDriver (assuming you've already set up the WebDriver)
driver = webdriver.Chrome()
driver.get("https://www.hatvp.fr/le-repertoire/liste-des-entites-enregistrees/")
# Function to wait for an element to be clickable
def wait_for_element_to_be_clickable(element_css_selector):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element_css_selector)))
# Assuming you know the maximum number of links or you find it dynamically
# You can also handle this with a while loop and break when no more links are found
max_links = 100 # Replace this with the actual number of links if known or a condition to break the loop
for i in range(1, max_links + 1):
try:
# Construct the selector for the current link
element_css_selector = f"a.orga-card-row:nth-child({i})"
# Wait for the element to be clickable
wait_for_element_to_be_clickable(element_css_selector)
# Click the element
driver.find_element_by_css_selector(element_css_selector).click()
# Add any additional code here to handle the navigation or interaction with the clicked link
# Navigate back to the main page if necessary
driver.back()
# Wait for the page to load or for a specific element to be present to ensure the state is ready for the next click
# WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "some_element_on_main_page")))
except Exception as e:
print(f"An error occurred: {e}")
# Handle the exception (e.g., element not found, page navigation issues, etc.)
# Consider breaking the loop or continuing to the next iteration
break
# Close the WebDriver
driver.quit()
replace max_links with the actual number of links you expect to click or implement a dynamic way to determine when to stop the loop.