Added example scripts for using selenium
This commit is contained in:
38
selenium/dom.py
Normal file
38
selenium/dom.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from selenium.webdriver.common.by import By
|
||||
import json
|
||||
|
||||
def connect_to_chrome():
|
||||
"""
|
||||
Connect to an existing Chrome browser instance and print the DOM
|
||||
"""
|
||||
# Chrome options for connecting to existing browser
|
||||
chrome_options = Options()
|
||||
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1234")
|
||||
|
||||
try:
|
||||
# Connect to the existing browser
|
||||
driver = webdriver.Chrome(options=chrome_options)
|
||||
|
||||
# Get the current page's DOM
|
||||
dom = driver.find_element(By.TAG_NAME, 'html').get_attribute('outerHTML')
|
||||
|
||||
# Pretty print the DOM
|
||||
parsed_dom = json.loads(json.dumps(dom))
|
||||
print(json.dumps(parsed_dom, indent=2))
|
||||
|
||||
return driver
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error connecting to Chrome: {str(e)}")
|
||||
print("\nMake sure Chrome is running with remote debugging enabled!")
|
||||
print("Start Chrome with: chrome.exe --remote-debugging-port=9222")
|
||||
return None
|
||||
|
||||
if __name__ == "__main__":
|
||||
driver = connect_to_chrome()
|
||||
if driver:
|
||||
# Keep the connection open until user input
|
||||
input("Press Enter to close the connection...")
|
||||
driver.quit()
|
||||
6
selenium/install.sh
Normal file
6
selenium/install.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
apt-get update && apt-get install gnupg wget -y && \
|
||||
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
|
||||
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
|
||||
apt-get update && \
|
||||
apt-get install google-chrome-stable -y --no-install-recommends && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
5
selenium/start.sh
Normal file
5
selenium/start.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
/opt/google/chrome/chrome \
|
||||
--no-sandbox \
|
||||
--headless=new \
|
||||
--remote-debugging-port=1234 \
|
||||
https://google.be &
|
||||
Reference in New Issue
Block a user