Python bindings for Apple's WebKit and JavaScriptCore frameworks on macOS, enabling web browser functionality and JavaScript execution in Python applications
—
This document covers the original WebView component and related classes that provide backward compatibility with older WebKit functionality. While these components have been superseded by the modern WKWebView system, they are still available and provide additional capabilities for DOM manipulation and legacy application support.
The original WebKit web view component for displaying web content.
class WebView:
def initWithFrame_frameName_groupName_(self, frame, frameName, groupName):
"""Initialize web view with frame, frame name, and group name."""
def canShowMIMEType_(self, MIMEType):
"""Check if the specified MIME type can be displayed."""
def goBack(self):
"""Navigate to the previous page in history."""
def goForward(self):
"""Navigate to the next page in history."""
def canGoBack(self):
"""Check if backward navigation is possible."""
def canGoForward(self):
"""Check if forward navigation is possible."""
def searchFor_direction_caseSensitive_wrap_(self, string, forward, caseFlag, wrapFlag):
"""Search for text within the page with options for direction, case sensitivity, and wrapping."""
def isLoading(self):
"""Check if the web view is currently loading content."""
def setDrawsBackground_(self, flag):
"""Control whether the web view draws its background."""
def drawsBackground(self):
"""Get the current background drawing state."""
def mainFrame(self):
"""Get the main frame of the web view."""
def selectedFrame(self):
"""Get the currently selected frame."""
def backForwardList(self):
"""Get the back/forward list for navigation history."""
def setMaintainsBackForwardList_(self, flag):
"""Set whether to maintain a back/forward list."""
def setHostWindow_(self, hostWindow):
"""Set the host window for the web view."""
def hostWindow(self):
"""Get the host window of the web view."""
def setGroupName_(self, groupName):
"""Set the group name for resource sharing."""
def groupName(self):
"""Get the group name."""
def estimatedProgress(self):
"""Get the estimated loading progress (0.0 to 1.0)."""
def isEditable(self):
"""Check if the web view content is editable."""
def setEditable_(self, flag):
"""Set the editable state of the web view."""
def setUIDelegate_(self, delegate):
"""Set the UI delegate for handling user interface callbacks."""
def UIDelegate(self):
"""Get the current UI delegate."""
def setResourceLoadDelegate_(self, delegate):
"""Set the resource load delegate for handling resource loading."""
def resourceLoadDelegate(self):
"""Get the current resource load delegate."""
def setDownloadDelegate_(self, delegate):
"""Set the download delegate for handling file downloads."""
def downloadDelegate(self):
"""Get the current download delegate."""
def setFrameLoadDelegate_(self, delegate):
"""Set the frame load delegate for handling frame loading events."""
def frameLoadDelegate(self):
"""Get the current frame load delegate."""
def setPolicyDelegate_(self, delegate):
"""Set the policy delegate for handling navigation policies."""
def policyDelegate(self):
"""Get the current policy delegate."""
def setPreferences_(self, preferences):
"""Set the preferences for the web view."""
def preferences(self):
"""Get the current preferences."""
def setPreferencesIdentifier_(self, anIdentifier):
"""Set the preferences identifier for shared preferences."""
def preferencesIdentifier(self):
"""Get the preferences identifier."""
def setCustomUserAgent_(self, userAgent):
"""Set a custom user agent string."""
def customUserAgent(self):
"""Get the custom user agent string."""
def userAgentForURL_(self, URL):
"""Get the user agent string for a specific URL."""
def supportsTextEncoding(self):
"""Check if text encoding changes are supported."""
def setCustomTextEncodingName_(self, encodingName):
"""Set a custom text encoding name."""
def customTextEncodingName(self):
"""Get the custom text encoding name."""
def setApplicationNameForUserAgent_(self, applicationName):
"""Set the application name for the user agent string."""
def applicationNameForUserAgent(self):
"""Get the application name from the user agent."""
def setMediaStyle_(self, mediaStyle):
"""Set the media style for CSS media queries."""
def mediaStyle(self):
"""Get the current media style."""
def stringByEvaluatingJavaScriptFromString_(self, script):
"""Execute JavaScript and return the result as a string."""
def windowScriptObject(self):
"""Get the window script object for JavaScript interaction."""Configuration settings for WebView behavior.
class WebPreferences:
def init(self):
"""Initialize web preferences with default settings."""
def initWithIdentifier_(self, anIdentifier):
"""Initialize with a specific identifier for shared preferences."""
def identifier(self):
"""Get the preferences identifier."""
def setStandardFontFamily_(self, family):
"""Set the standard font family."""
def standardFontFamily(self):
"""Get the standard font family."""
def setFixedFontFamily_(self, family):
"""Set the fixed-width font family."""
def fixedFontFamily(self):
"""Get the fixed-width font family."""
def setSerifFontFamily_(self, family):
"""Set the serif font family."""
def serifFontFamily(self):
"""Get the serif font family."""
def setSansSerifFontFamily_(self, family):
"""Set the sans-serif font family."""
def sansSerifFontFamily(self):
"""Get the sans-serif font family."""
def setCursiveFontFamily_(self, family):
"""Set the cursive font family."""
def cursiveFontFamily(self):
"""Get the cursive font family."""
def setFantasyFontFamily_(self, family):
"""Set the fantasy font family."""
def fantasyFontFamily(self):
"""Get the fantasy font family."""
def setDefaultFontSize_(self, size):
"""Set the default font size."""
def defaultFontSize(self):
"""Get the default font size."""
def setDefaultFixedFontSize_(self, size):
"""Set the default fixed font size."""
def defaultFixedFontSize(self):
"""Get the default fixed font size."""
def setMinimumFontSize_(self, size):
"""Set the minimum font size."""
def minimumFontSize(self):
"""Get the minimum font size."""
def setMinimumLogicalFontSize_(self, size):
"""Set the minimum logical font size."""
def minimumLogicalFontSize(self):
"""Get the minimum logical font size."""
def setDefaultTextEncodingName_(self, encoding):
"""Set the default text encoding name."""
def defaultTextEncodingName(self):
"""Get the default text encoding name."""
def setUserStyleSheetEnabled_(self, flag):
"""Enable or disable user style sheet."""
def userStyleSheetEnabled(self):
"""Check if user style sheet is enabled."""
def setUserStyleSheetLocation_(self, URL):
"""Set the location of the user style sheet."""
def userStyleSheetLocation(self):
"""Get the user style sheet location."""
def setJavaEnabled_(self, flag):
"""Enable or disable Java support."""
def isJavaEnabled(self):
"""Check if Java is enabled."""
def setJavaScriptEnabled_(self, flag):
"""Enable or disable JavaScript."""
def isJavaScriptEnabled(self):
"""Check if JavaScript is enabled."""
def setJavaScriptCanOpenWindowsAutomatically_(self, flag):
"""Set whether JavaScript can open windows automatically."""
def javaScriptCanOpenWindowsAutomatically(self):
"""Check if JavaScript can open windows automatically."""
def setPlugInsEnabled_(self, flag):
"""Enable or disable plugins."""
def arePlugInsEnabled(self):
"""Check if plugins are enabled."""
def setAllowsAnimatedImages_(self, flag):
"""Set whether animated images are allowed."""
def allowsAnimatedImages(self):
"""Check if animated images are allowed."""
def setAllowsAnimatedImageLooping_(self, flag):
"""Set whether animated image looping is allowed."""
def allowsAnimatedImageLooping(self):
"""Check if animated image looping is allowed."""
def setLoadsImagesAutomatically_(self, flag):
"""Set whether images load automatically."""
def loadsImagesAutomatically(self):
"""Check if images load automatically."""
def setAutosaves_(self, flag):
"""Set whether preferences autosave."""
def autosaves(self):
"""Check if preferences autosave."""
def setShouldPrintBackgrounds_(self, flag):
"""Set whether backgrounds should print."""
def shouldPrintBackgrounds(self):
"""Check if backgrounds should print."""
def setPrivateBrowsingEnabled_(self, flag):
"""Enable or disable private browsing."""
def privateBrowsingEnabled(self):
"""Check if private browsing is enabled."""
def setTabsToLinks_(self, flag):
"""Set whether tab key moves to links."""
def tabsToLinks(self):
"""Check if tab key moves to links."""
def setUsesPageCache_(self, flag):
"""Set whether to use page cache."""
def usesPageCache(self):
"""Check if page cache is used."""
def setCacheModel_(self, cacheModel):
"""Set the cache model."""
def cacheModel(self):
"""Get the cache model."""
def setSuppressIncrementalRendering_(self, flag):
"""Set whether to suppress incremental rendering."""
def suppressIncrementalRendering(self):
"""Check if incremental rendering is suppressed."""Interface between JavaScript and Python objects in legacy WebView.
class WebScriptObject:
def callWebScriptMethod_withArguments_(self, name, arguments):
"""Call a JavaScript method with arguments."""
def evaluateWebScript_(self, script):
"""Evaluate JavaScript code."""
def setWebScriptValueAtIndex_value_(self, index, value):
"""Set a JavaScript array element value."""
def webScriptValueAtIndex_(self, index):
"""Get a JavaScript array element value."""
def setException_(self, description):
"""Set a JavaScript exception."""
def stringRepresentation(self):
"""Get string representation of the object."""
def JSObject(self):
"""Get the underlying JavaScript object."""Legacy navigation type constants for WebView.
# Web Navigation Types (Legacy)
WebNavigationTypeLinkClicked = 0
WebNavigationTypeFormSubmitted = 1
WebNavigationTypeBackForward = 2
WebNavigationTypeReload = 3
WebNavigationTypeFormResubmitted = 4
WebNavigationTypeOther = 5import WebKit
from Foundation import NSRect, NSURL, NSURLRequest
# Create web view with legacy WebView
frame = NSRect((0, 0), (800, 600))
web_view = WebKit.WebView.alloc().initWithFrame_frameName_groupName_(
frame, None, None
)
# Configure preferences
prefs = WebKit.WebPreferences.alloc().init()
prefs.setJavaScriptEnabled_(True)
prefs.setJavaEnabled_(False)
web_view.setPreferences_(prefs)
# Load a URL
url = NSURL.URLWithString_("https://www.example.com")
request = NSURLRequest.requestWithURL_(url)
web_view.mainFrame().loadRequest_(request)
# Execute JavaScript
result = web_view.stringByEvaluatingJavaScriptFromString_("document.title")
print(f"Page title: {result}")When migrating from legacy WebView to modern WKWebView:
WebView with WKWebViewWKWebViewConfiguration instead of WebPreferencesstringByEvaluatingJavaScriptFromString_ with evaluateJavaScript_completionHandler_Legacy WebView components are maintained for backward compatibility but new development should use the modern WKWebView system whenever possible.
Install with Tessl CLI
npx tessl i tessl/pypi-pyobjc-framework-webkit