-1

I currently try to create a loop to click all links on a website https://www.hatvp.fr/le-repertoire/liste-des-entites-enregistrees/ one after another.

I found this part in the html code that could solve my problem: a.orga-card-row:nth-child(i) it's from the css selector, and increases with every item so (1),(2) etc., so it should be easy to create a for i in range or while loop. A earlier version with the xpath ran smoothly, but I have no clue how to create a loop with it, so this part would be a easier solution. Another problem is the variable in the call, I solved this with a str(i), but since the first part of the code doesn't work, I don't know if that's sufficient.

i=1

def wait_for_element_to_be_clickable(element):
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element)))

element = "a.orga-card-row:nth-child("+str(i)+")"
wait_for_element_to_be_clickable(element)
driver.find_element_by_css_selector(element).click()

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.