Код: Выделить всё
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
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import pandas as pd
import time
# Setup Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized") # Maximize the browser window
# Initialize Selenium WebDriver using ChromeDriverManager
driver = webdriver.Chrome(service=webdriver.chrome.service.Service(ChromeDriverManager().install()), options=chrome_options)
# Initialize an empty list to store company data
company_data = []
# Function to wait and click with retry
def wait_and_click(driver, by, value, retries=3):
for _ in range(retries):
try:
element = WebDriverWait(driver,20).until(EC.element_to_be_clickable((by, value)))
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
return True
except Exception as e:
print(f"Error clicking element {value}: {e}")
time.sleep(2) # Wait before retrying
return False
# Open the NFRC website
driver.get("https://www.nfrc.co.uk/search-members")
# Click on "Search for a roofing contractor now"
wait_and_click(driver, By.LINK_TEXT, "Search for a roofing contractor now")
# Click to open the dropdown for contractor type selection
dropdown_locator = (By.CLASS_NAME, "sfDropdownList")
wait_and_click(driver, By.CLASS_NAME, "sfDropdownList")
# Select "For a Domestic Property" from the dropdown
wait_and_click(driver, By.XPATH, '//[@id="MainContent_C001_contractorType"]/option[@value="CONTRACTOR_ADVANCED_DOMESTIC_PROPERTY_VIEW"]')
# Select "Pitched Roof"
wait_and_click(driver, By.XPATH, '//*[@id="MainContent_C001_rblRoofType"]/li[1]/label')
# Proceed to the next step
wait_and_click(driver, By.ID, "MainContent_C001_btnSearch")
# Add a delay to ensure the page loads completely after
clicking search button
time.sleep(5) # Adjust the time delay as needed
Подробнее здесь: https://stackoverflow.com/questions/786 ... back-links