from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
# Create a new profile folder in the current script directory
profile_dir = os.path.abspath("selenium_profile")
os.makedirs(profile_dir, exist_ok=True)
# URL stored as a string
url = "https://example.com" # Insert the actual URL here
adguard_extension_path = r"C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/Extensions/bgnkhhnnamicmpeenaelnjfhikgbkllg/5.2.80_0"
# Set Chrome options (modify as needed)
options = Options()
options.add_argument("--start-maximized")
options.add_argument(f"--user-data-dir={profile_dir}")
options.add_argument(f"--load-extension={adguard_extension_path}")
options.add_argument(f"--disable-extensions-except={adguard_extension_path}")
# options.add_argument("--headless") # Use this to run browser in headless mode
# Set driver path (modify if needed)
# service = Service("/path/to/chromedriver") # Set chromedriver path
# Launch the driver
driver = webdriver.Chrome(options=options)
try:
# Open the URL
driver.get(url)
# Wait until the button loads (up to 10 seconds)
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.drop-btn.second")))
# Click the button
button.click()
print("Button clicked successfully!")
except Exception as e:
print(f"Error occurred: {e}")
finally:
print(driver.capabilities.get("chrome", {}))
time.sleep(3) # Wait to observe the result (optional)
driver.quit()
Как запустить расширение AdGuard в WebDriver?
Вывод следующий.
Как мне изменить код? Версия манифеста.json — 3, а папка расширения AdGuard Chrome выглядит следующим образом: (расположение: C:\Users\USER_NAME\AppData\Local\Google\Chrome\User Data\Default\Extensions\bgnkhhnnamicmpeenaelnjfhikgbkllg\5.2.80_0)
Каталог
[code]from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import os
# Create a new profile folder in the current script directory profile_dir = os.path.abspath("selenium_profile") os.makedirs(profile_dir, exist_ok=True)
# URL stored as a string url = "https://example.com" # Insert the actual URL here adguard_extension_path = r"C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/Extensions/bgnkhhnnamicmpeenaelnjfhikgbkllg/5.2.80_0"
# Set Chrome options (modify as needed) options = Options() options.add_argument("--start-maximized") options.add_argument(f"--user-data-dir={profile_dir}") options.add_argument(f"--load-extension={adguard_extension_path}") options.add_argument(f"--disable-extensions-except={adguard_extension_path}") # options.add_argument("--headless") # Use this to run browser in headless mode
# Set driver path (modify if needed) # service = Service("/path/to/chromedriver") # Set chromedriver path
# Launch the driver driver = webdriver.Chrome(options=options)
try: # Open the URL driver.get(url)
# Wait until the button loads (up to 10 seconds) wait = WebDriverWait(driver, 10) button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.drop-btn.second")))
# Click the button button.click() print("Button clicked successfully!")
except Exception as e: print(f"Error occurred: {e}")
time.sleep(3) # Wait to observe the result (optional) driver.quit()
[/code] Как запустить расширение AdGuard в WebDriver? Вывод следующий. [code]Button clicked successfully! {'chromedriverVersion': '141.0.7390.76 (d6fcfbb51aac4df61d06692d4a62fd8b9aaf3c3a-refs/branch-heads/7390_65@{#3})', 'userDataDir': 'C:\\Users\\USER_NAME\\OneDrive\\Documents\\Python\\sel\\selenium_profile'} [Finished in 11.7s] [/code] Как мне изменить код? Версия манифеста.json — 3, а папка расширения AdGuard Chrome выглядит следующим образом: (расположение: C:\Users\USER_NAME\AppData\Local\Google\Chrome\User Data\Default\Extensions\bgnkhhnnamicmpeenaelnjfhikgbkllg\5.2.80_0) Каталог