Skip to content

Instantly share code, notes, and snippets.

@khancyr
Created March 1, 2024 15:39
Show Gist options
  • Save khancyr/78d9966cd8ff6b857501bc2d2dc1b4e4 to your computer and use it in GitHub Desktop.
Save khancyr/78d9966cd8ff6b857501bc2d2dc1b4e4 to your computer and use it in GitHub Desktop.
selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
import time
url = 'https://www.thejournal.ie/food-delivery-by-drone-6314671-Mar2024/'
for i in range(20):
try:
# Initialize the Chrome driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Navigate to the URL
driver.get(url)
input_id = 'poll-6314671-3'
#html_source = driver.page_source
#print(html_source)
# Locate the input element by its ID and click it
element = driver.find_element(By.ID, "poll-6314671-3")
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)
print(f"Input element clicked on iteration {i+1}.")
vote_button_selector = 'buttonVote'
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, vote_button_selector))).click()
print(f"Vote button clicked on iteration {i+1}.")
#time.sleep(20)
driver.delete_all_cookies()
print(f"Cookies cleared on iteration {i+1}.")
except TimeoutException as e:
print(f"Failed on iteration {i+1}: Element was not clickable within the timeout period. Error: {e}")
except Exception as e:
print(f"Error on iteration {i+1}: {e}")
finally:
# Close the browser after each iteration
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment