Skip to content

Instantly share code, notes, and snippets.

@7effrey89
Last active September 20, 2024 10:33
Show Gist options
  • Select an option

  • Save 7effrey89/e162307e04390fe769ceee95fc81c06c to your computer and use it in GitHub Desktop.

Select an option

Save 7effrey89/e162307e04390fe769ceee95fc81c06c to your computer and use it in GitHub Desktop.
Python web automation using Selenium and Edge to interact and print sharepoint webpage.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#selenium 4.23.1 was used for this code
#edge browser: Version 127.0.2651.105 (Official build) (64-bit)
#edge driver: Version 127.0.2651.105 (Stable Channel) (64-bit)
# Define the URL to be saved
url = 'https://microsoft.sharepoint.com/sites/FabCon/SitePages/FabCon-Vegas-2024.aspx'
# Set up Selenium WebDriver for Edge
options = EdgeOptions()
options.use_chromium = True
# Update with the path to your EdgeDriver
edge_driver_path = r"C:\Users\jeffreylai\OneDrive - Microsoft\Desktop\edgedriver_win64\msedgedriver.exe"
service = Service(executable_path=edge_driver_path)
# Initialize the Edge WebDriver
driver = webdriver.Edge(service=service, options=options)
# Navigate to the URL
driver.get(url)
#You are prompted to sign in to your Microsoft Edge browser profile when you are visiting sharepoint online through web automation: I was faced with this message:
#For the best browsing experience while accessing a service, app or website,
# we recommend signing in to your Microsoft Edge browser profile using jeffreylai@microsoft.com.
#When you use the browser's work profile, your organization can view some of your data.Learn More
# Wait for the "Continue with current profile" button to be clickable
#This is the code that I used to click the "Continue with current profile" button:
continue_button = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//input[@type="submit" and @class="proxyweb-secondary-link" and @value="Continue with current profile"]'))
)
continue_button.click()
# Wait for the next page to load by waiting for a specific element on the next page
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.TAG_NAME, 'body')) # Adjust this selector as needed
)
# Fetch the content from the URL
html_content = driver.page_source
# Close the driver
driver.quit()
# Print the fetched HTML content
print(html_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment