Skip to content

Instantly share code, notes, and snippets.

@jepler
Created December 2, 2024 14:13
Show Gist options
  • Save jepler/cb64d065a1ad30137cd2e4cc1620c858 to your computer and use it in GitHub Desktop.
Save jepler/cb64d065a1ad30137cd2e4cc1620c858 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
# Configure Chrome options
options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1200") # Set the window size
# Set the path to the Chromedriver
DRIVER_PATH = '/usr/bin/chromedriver'
# Initialize the Chrome driver with the specified options
driver = webdriver.Chrome(options=options)
# Your code here to interact with the page
# ...
# Navigate to Hacker News
driver.get("https://emergent.unpythonic.net/")
# My page doesn't javascript anything so this is redundant, but ...
try:
delay = 3 # seconds
WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'topbar')))
print("Page is ready!")
except TimeoutException:
print("Loading took too much time!")
# Find the first link, whatever it is
link = driver.find_element("xpath", "//a")
# print something about it
print(link.text)
print(link.get_property("href"))
# It's a good practice to close the driver when you're finished
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment