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()
|
||||
Reference in New Issue
Block a user