Created
July 15, 2022 00:51
-
-
Save SKaplanOfficial/8473fd8bf0a47468277688c1c481a552 to your computer and use it in GitHub Desktop.
Using PyXA to access Safari methods and properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyXA | |
# Open URL in new tab | |
safari = PyXA.application("Safari") | |
# Get open windows, documents, and tabs | |
window1 = safari.front_window() | |
window2 = safari.windows()[1] | |
documents = safari.documents() | |
current_doc = safari.current_document | |
tabs = window1.tabs() | |
current_tab = window1.current_tab | |
# Get properties of documents | |
urls = documents.url() | |
names = documents.name() | |
html = current_doc.source() | |
# Get properties of tabs | |
urls = tabs.url() | |
texts = tabs.text() | |
name = current_tab.name() | |
# Filter documents and tabs | |
doc1 = documents.by_url("https://apple.com") | |
doc2 = documents.by_name("Apple") | |
tab1 = tabs.by_index(1) | |
tab2 = tabs.by_visible(True) | |
# Bulk document operations | |
documents.add_to_reading_list() | |
documents.email() | |
documents.do_javascript("alert('Testing 1 2 3');") | |
documents.search("Example") | |
# Bulk tab operations | |
tabs.reload() | |
tabs.add_to_reading_list() | |
tabs.email() | |
tabs.do_javascript("alert('Hello!');") | |
tabs.search("Example") | |
tabs.move_to(window2) | |
tabs.duplicate_to(window2) | |
# Sub-array operations | |
some_tabs = tabs[3:5] | |
some_tabs.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment