This is my whole script
import time
import pandas as pd
import locale
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from datetime import datetime
from decimal import *
locale.setlocale(locale.LC_ALL, 'nl_NL.UTF-8')
from selenium.webdriver.edge.options import Options
options = Options()
options.headless = True
MAX_WAIT = 10
def deal_with_cookies(driver):
element = WebDriverWait(driver, MAX_WAIT).until(EC.presence_of_element_located((By.XPATH, '//*[@id="gdpr-consent-notice"]')))
driver.switch_to.frame(element)
button = WebDriverWait(driver, MAX_WAIT).until(EC.element_to_be_clickable((By.XPATH, '//button[@id="save"]')))
button.send_keys(Keys.ENTER)
driver.switch_to.default_content()
def click_volgende(driver):
button = driver.find_element(By.ID, 'ctl00_ctl00_ctl00_ContentPlaceHolder1_LeftContent_Content_AdviesList_cmdNext')
button.click()
def get_table_data(driver):
start_time = time.time()
try:
table = driver.find_element(By.CLASS_NAME, 'ContentTable')
rows = table.find_elements_by_tag_name('tr')
row_num = 0
l = []
for row in rows:
if row_num > 0:
td = row.find_elements(By.TAG_NAME, "td")
row2 = [row.text for row in td]
l.append(row2)
row_num = row_num + 1
df = pd.DataFrame(l, columns=["A", "B", "C", "D", "E"],dtype=float)
print(df)
except (AssertionError, WebDriverException) as e:
if time.time() - start_time > MAX_WAIT:
print("Unable to find element, ending script")
raise e
time.sleep(0.5)
def main():
driver = webdriver.Edge(options=options)
driver.get('https://www.guruwatch.nl/aandeel/330011556/Boeing-Company/Adviezen.aspx')
deal_with_cookies(driver)
for page in range(1,2):
print(f"Scraping page {page}.")
get_table_data(driver)
# click_volgende(driver)
time.sleep(5)
driver.quit()
if __name__ == "__main__":
main()
I just copy and paste it from VSC, where it is working perfectly