0
# DOM Manipulation
1
2
Complete Document Object Model classes for programmatic web content manipulation. Provides access to HTML elements, events, CSS styles, and document structure through Python bindings to WebKit's DOM implementation.
3
4
## Capabilities
5
6
### Document Access
7
8
#### DOMDocument
9
10
Root document object providing access to the entire DOM tree and document-level operations.
11
12
```python { .api }
13
class DOMDocument:
14
# Element creation and access
15
def createElement_(self, tagName: str) -> DOMElement: ...
16
def createTextNode_(self, data: str) -> DOMText: ...
17
def createComment_(self, data: str) -> DOMComment: ...
18
def createDocumentFragment(self) -> DOMDocumentFragment: ...
19
def getElementById_(self, elementId: str) -> DOMElement: ...
20
def getElementsByTagName_(self, tagName: str) -> DOMNodeList: ...
21
def getElementsByClassName_(self, className: str) -> DOMNodeList: ...
22
23
# Document properties
24
@property
25
def documentElement(self) -> DOMElement: ...
26
@property
27
def body(self) -> DOMHTMLElement: ...
28
@property
29
def head(self) -> DOMHTMLElement: ...
30
@property
31
def title(self) -> str: ...
32
@property
33
def URL(self) -> str: ...
34
@property
35
def domain(self) -> str: ...
36
@property
37
def cookie(self) -> str: ...
38
@property
39
def lastModified(self) -> str: ...
40
@property
41
def characterSet(self) -> str: ...
42
@property
43
def contentType(self) -> str: ...
44
@property
45
def doctype(self) -> DOMDocumentType: ...
46
47
# Document manipulation
48
def importNode_deep_(self, importedNode: DOMNode, deep: bool) -> DOMNode: ...
49
def adoptNode_(self, source: DOMNode) -> DOMNode: ...
50
51
# Focus and editing
52
def hasFocus(self) -> bool: ...
53
def execCommand_(self, command: str) -> bool: ...
54
def execCommand_userInterface_value_(self, command: str, userInterface: bool, value: str) -> bool: ...
55
def queryCommandEnabled_(self, command: str) -> bool: ...
56
def queryCommandIndeterm_(self, command: str) -> bool: ...
57
def queryCommandState_(self, command: str) -> bool: ...
58
def queryCommandSupported_(self, command: str) -> bool: ...
59
def queryCommandValue_(self, command: str) -> str: ...
60
61
# Ranges and selection
62
def createRange(self) -> DOMRange: ...
63
def getSelection(self) -> DOMSelection: ...
64
65
# Event handling
66
def addEventListener_listener_useCapture_(self, type: str, listener: callable, useCapture: bool): ...
67
def removeEventListener_listener_useCapture_(self, type: str, listener: callable, useCapture: bool): ...
68
69
# CSS and styles
70
def getComputedStyle_pseudoElement_(self, element: DOMElement, pseudoElement: str) -> DOMCSSStyleDeclaration: ...
71
def getMatchedCSSRules_pseudoElement_authorOnly_(self, element: DOMElement, pseudoElement: str, authorOnly: bool) -> DOMCSSRuleList: ...
72
```
73
74
#### DOMHTMLDocument
75
76
HTML-specific document functionality extending DOMDocument.
77
78
```python { .api }
79
class DOMHTMLDocument(DOMDocument):
80
def open(self): ...
81
def close(self): ...
82
def write_(self, text: str): ...
83
def writeln_(self, text: str): ...
84
85
@property
86
def images(self) -> DOMHTMLCollection: ...
87
@property
88
def applets(self) -> DOMHTMLCollection: ...
89
@property
90
def links(self) -> DOMHTMLCollection: ...
91
@property
92
def forms(self) -> DOMHTMLCollection: ...
93
@property
94
def anchors(self) -> DOMHTMLCollection: ...
95
@property
96
def embeds(self) -> DOMHTMLCollection: ...
97
@property
98
def plugins(self) -> DOMHTMLCollection: ...
99
@property
100
def scripts(self) -> DOMHTMLCollection: ...
101
102
def hasFocus(self) -> bool: ...
103
```
104
105
### Element Manipulation
106
107
#### DOMElement
108
109
Base element class for all HTML and XML elements.
110
111
```python { .api }
112
class DOMElement(DOMNode):
113
# Attribute access
114
def getAttribute_(self, name: str) -> str: ...
115
def setAttribute_value_(self, name: str, value: str): ...
116
def removeAttribute_(self, name: str): ...
117
def hasAttribute_(self, name: str) -> bool: ...
118
def getAttributeNode_(self, name: str) -> DOMAttr: ...
119
def setAttributeNode_(self, newAttr: DOMAttr) -> DOMAttr: ...
120
def removeAttributeNode_(self, oldAttr: DOMAttr) -> DOMAttr: ...
121
122
# Namespaced attributes
123
def getAttributeNS_localName_(self, namespaceURI: str, localName: str) -> str: ...
124
def setAttributeNS_qualifiedName_value_(self, namespaceURI: str, qualifiedName: str, value: str): ...
125
def removeAttributeNS_localName_(self, namespaceURI: str, localName: str): ...
126
def hasAttributeNS_localName_(self, namespaceURI: str, localName: str) -> bool: ...
127
def getAttributeNodeNS_localName_(self, namespaceURI: str, localName: str) -> DOMAttr: ...
128
def setAttributeNodeNS_(self, newAttr: DOMAttr) -> DOMAttr: ...
129
130
# Element properties
131
@property
132
def tagName(self) -> str: ...
133
@property
134
def id(self) -> str: ...
135
@property
136
def className(self) -> str: ...
137
@property
138
def innerHTML(self) -> str: ...
139
@property
140
def outerHTML(self) -> str: ...
141
@property
142
def innerText(self) -> str: ...
143
@property
144
def outerText(self) -> str: ...
145
146
# Element traversal
147
def getElementsByTagName_(self, name: str) -> DOMNodeList: ...
148
def getElementsByTagNameNS_localName_(self, namespaceURI: str, localName: str) -> DOMNodeList: ...
149
def getElementsByClassName_(self, name: str) -> DOMNodeList: ...
150
151
# Element positioning and size
152
@property
153
def clientWidth(self) -> int: ...
154
@property
155
def clientHeight(self) -> int: ...
156
@property
157
def scrollWidth(self) -> int: ...
158
@property
159
def scrollHeight(self) -> int: ...
160
@property
161
def scrollLeft(self) -> int: ...
162
@property
163
def scrollTop(self) -> int: ...
164
@property
165
def offsetWidth(self) -> int: ...
166
@property
167
def offsetHeight(self) -> int: ...
168
@property
169
def offsetLeft(self) -> int: ...
170
@property
171
def offsetTop(self) -> int: ...
172
@property
173
def offsetParent(self) -> DOMElement: ...
174
175
# Scrolling
176
def scrollIntoView_(self, alignToTop: bool): ...
177
def scrollIntoViewIfNeeded_(self, centerIfNeeded: bool): ...
178
def scrollByLines_(self, lines: int): ...
179
def scrollByPages_(self, pages: int): ...
180
181
# Element queries
182
def querySelector_(self, selectors: str) -> DOMElement: ...
183
def querySelectorAll_(self, selectors: str) -> DOMNodeList: ...
184
185
# Element relationships
186
def contains_(self, element: DOMElement) -> bool: ...
187
def webkitMatchesSelector_(self, selectors: str) -> bool: ...
188
189
# Style access
190
@property
191
def style(self) -> DOMCSSStyleDeclaration: ...
192
```
193
194
#### DOMHTMLElement
195
196
Base class for HTML elements with HTML-specific functionality.
197
198
```python { .api }
199
class DOMHTMLElement(DOMElement):
200
@property
201
def title(self) -> str: ...
202
@property
203
def lang(self) -> str: ...
204
@property
205
def dir(self) -> str: ...
206
@property
207
def tabIndex(self) -> int: ...
208
@property
209
def accessKey(self) -> str: ...
210
@property
211
def contentEditable(self) -> str: ...
212
def isContentEditable(self) -> bool: ...
213
@property
214
def draggable(self) -> bool: ...
215
@property
216
def spellcheck(self) -> bool: ...
217
@property
218
def hidden(self) -> bool: ...
219
220
# Focus management
221
def focus(self): ...
222
def blur(self): ...
223
def click(self): ...
224
```
225
226
### HTML Form Elements
227
228
#### DOMHTMLInputElement
229
230
HTML input element with form-specific functionality.
231
232
```python { .api }
233
class DOMHTMLInputElement(DOMHTMLElement):
234
@property
235
def type(self) -> str: ...
236
@property
237
def name(self) -> str: ...
238
@property
239
def value(self) -> str: ...
240
@property
241
def checked(self) -> bool: ...
242
@property
243
def defaultChecked(self) -> bool: ...
244
@property
245
def disabled(self) -> bool: ...
246
@property
247
def readOnly(self) -> bool: ...
248
@property
249
def maxLength(self) -> int: ...
250
@property
251
def size(self) -> int: ...
252
@property
253
def alt(self) -> str: ...
254
@property
255
def src(self) -> str: ...
256
@property
257
def useMap(self) -> str: ...
258
@property
259
def accept(self) -> str: ...
260
@property
261
def accessKey(self) -> str: ...
262
@property
263
def align(self) -> str: ...
264
@property
265
def defaultValue(self) -> str: ...
266
@property
267
def form(self) -> DOMHTMLFormElement: ...
268
@property
269
def selectionStart(self) -> int: ...
270
@property
271
def selectionEnd(self) -> int: ...
272
@property
273
def autofocus(self) -> bool: ...
274
@property
275
def multiple(self) -> bool: ...
276
@property
277
def placeholder(self) -> str: ...
278
@property
279
def required(self) -> bool: ...
280
@property
281
def autocomplete(self) -> str: ...
282
@property
283
def files(self) -> DOMFileList: ...
284
285
def select(self): ...
286
def setSelectionRange_end_(self, start: int, end: int): ...
287
def click(self): ...
288
```
289
290
#### DOMHTMLSelectElement
291
292
HTML select element for dropdown lists.
293
294
```python { .api }
295
class DOMHTMLSelectElement(DOMHTMLElement):
296
@property
297
def type(self) -> str: ...
298
@property
299
def selectedIndex(self) -> int: ...
300
@property
301
def value(self) -> str: ...
302
@property
303
def length(self) -> int: ...
304
@property
305
def form(self) -> DOMHTMLFormElement: ...
306
@property
307
def options(self) -> DOMHTMLOptionsCollection: ...
308
@property
309
def disabled(self) -> bool: ...
310
@property
311
def multiple(self) -> bool: ...
312
@property
313
def name(self) -> str: ...
314
@property
315
def size(self) -> int: ...
316
@property
317
def tabIndex(self) -> int: ...
318
@property
319
def autofocus(self) -> bool: ...
320
@property
321
def required(self) -> bool: ...
322
323
def add_before_(self, element: DOMHTMLElement, before: DOMHTMLElement): ...
324
def remove_(self, index: int): ...
325
def namedItem_(self, name: str) -> DOMNode: ...
326
def item_(self, index: int) -> DOMNode: ...
327
```
328
329
#### DOMHTMLOptionElement
330
331
HTML option element for select lists.
332
333
```python { .api }
334
class DOMHTMLOptionElement(DOMHTMLElement):
335
@property
336
def form(self) -> DOMHTMLFormElement: ...
337
@property
338
def defaultSelected(self) -> bool: ...
339
@property
340
def text(self) -> str: ...
341
@property
342
def index(self) -> int: ...
343
@property
344
def disabled(self) -> bool: ...
345
@property
346
def label(self) -> str: ...
347
@property
348
def selected(self) -> bool: ...
349
@property
350
def value(self) -> str: ...
351
```
352
353
#### DOMHTMLTextAreaElement
354
355
HTML textarea element for multi-line text input.
356
357
```python { .api }
358
class DOMHTMLTextAreaElement(DOMHTMLElement):
359
@property
360
def defaultValue(self) -> str: ...
361
@property
362
def form(self) -> DOMHTMLFormElement: ...
363
@property
364
def accessKey(self) -> str: ...
365
@property
366
def cols(self) -> int: ...
367
@property
368
def disabled(self) -> bool: ...
369
@property
370
def name(self) -> str: ...
371
@property
372
def readOnly(self) -> bool: ...
373
@property
374
def rows(self) -> int: ...
375
@property
376
def tabIndex(self) -> int: ...
377
@property
378
def type(self) -> str: ...
379
@property
380
def value(self) -> str: ...
381
@property
382
def selectionStart(self) -> int: ...
383
@property
384
def selectionEnd(self) -> int: ...
385
@property
386
def textLength(self) -> int: ...
387
@property
388
def autofocus(self) -> bool: ...
389
@property
390
def placeholder(self) -> str: ...
391
@property
392
def required(self) -> bool: ...
393
@property
394
def wrap(self) -> str: ...
395
396
def select(self): ...
397
def setSelectionRange_end_(self, start: int, end: int): ...
398
```
399
400
#### DOMHTMLButtonElement
401
402
HTML button element.
403
404
```python { .api }
405
class DOMHTMLButtonElement(DOMHTMLElement):
406
@property
407
def form(self) -> DOMHTMLFormElement: ...
408
@property
409
def accessKey(self) -> str: ...
410
@property
411
def disabled(self) -> bool: ...
412
@property
413
def name(self) -> str: ...
414
@property
415
def tabIndex(self) -> int: ...
416
@property
417
def type(self) -> str: ...
418
@property
419
def value(self) -> str: ...
420
@property
421
def autofocus(self) -> bool: ...
422
def willValidate(self) -> bool: ...
423
def setWillValidate_(self, willValidate: bool): ...
424
```
425
426
### Other HTML Elements
427
428
#### DOMHTMLImageElement
429
430
HTML image element.
431
432
```python { .api }
433
class DOMHTMLImageElement(DOMHTMLElement):
434
@property
435
def name(self) -> str: ...
436
@property
437
def align(self) -> str: ...
438
@property
439
def alt(self) -> str: ...
440
@property
441
def border(self) -> str: ...
442
@property
443
def height(self) -> int: ...
444
@property
445
def hspace(self) -> int: ...
446
@property
447
def isMap(self) -> bool: ...
448
@property
449
def longDesc(self) -> str: ...
450
@property
451
def src(self) -> str: ...
452
@property
453
def useMap(self) -> str: ...
454
@property
455
def vspace(self) -> int: ...
456
@property
457
def width(self) -> int: ...
458
@property
459
def complete(self) -> bool: ...
460
@property
461
def lowsrc(self) -> str: ...
462
@property
463
def naturalHeight(self) -> int: ...
464
@property
465
def naturalWidth(self) -> int: ...
466
@property
467
def x(self) -> int: ...
468
@property
469
def y(self) -> int: ...
470
```
471
472
#### DOMHTMLLinkElement
473
474
HTML link element.
475
476
```python { .api }
477
class DOMHTMLLinkElement(DOMHTMLElement):
478
@property
479
def disabled(self) -> bool: ...
480
@property
481
def charset(self) -> str: ...
482
@property
483
def href(self) -> str: ...
484
@property
485
def hreflang(self) -> str: ...
486
@property
487
def media(self) -> str: ...
488
@property
489
def rel(self) -> str: ...
490
@property
491
def rev(self) -> str: ...
492
@property
493
def target(self) -> str: ...
494
@property
495
def type(self) -> str: ...
496
@property
497
def sheet(self) -> DOMStyleSheet: ...
498
```
499
500
### Node Hierarchy
501
502
#### DOMNode
503
504
Base node class for all DOM objects.
505
506
```python { .api }
507
class DOMNode:
508
# Node types
509
ELEMENT_NODE = 1
510
ATTRIBUTE_NODE = 2
511
TEXT_NODE = 3
512
CDATA_SECTION_NODE = 4
513
ENTITY_REFERENCE_NODE = 5
514
ENTITY_NODE = 6
515
PROCESSING_INSTRUCTION_NODE = 7
516
COMMENT_NODE = 8
517
DOCUMENT_NODE = 9
518
DOCUMENT_TYPE_NODE = 10
519
DOCUMENT_FRAGMENT_NODE = 11
520
NOTATION_NODE = 12
521
522
# Node properties
523
@property
524
def nodeName(self) -> str: ...
525
@property
526
def nodeValue(self) -> str: ...
527
@property
528
def nodeType(self) -> int: ...
529
@property
530
def parentNode(self) -> DOMNode: ...
531
@property
532
def childNodes(self) -> DOMNodeList: ...
533
@property
534
def firstChild(self) -> DOMNode: ...
535
@property
536
def lastChild(self) -> DOMNode: ...
537
@property
538
def previousSibling(self) -> DOMNode: ...
539
@property
540
def nextSibling(self) -> DOMNode: ...
541
@property
542
def attributes(self) -> DOMNamedNodeMap: ...
543
@property
544
def ownerDocument(self) -> DOMDocument: ...
545
@property
546
def namespaceURI(self) -> str: ...
547
@property
548
def prefix(self) -> str: ...
549
@property
550
def localName(self) -> str: ...
551
@property
552
def textContent(self) -> str: ...
553
554
# Node manipulation
555
def insertBefore_refChild_(self, newChild: DOMNode, refChild: DOMNode) -> DOMNode: ...
556
def replaceChild_oldChild_(self, newChild: DOMNode, oldChild: DOMNode) -> DOMNode: ...
557
def removeChild_(self, oldChild: DOMNode) -> DOMNode: ...
558
def appendChild_(self, newChild: DOMNode) -> DOMNode: ...
559
def hasChildNodes(self) -> bool: ...
560
def cloneNode_(self, deep: bool) -> DOMNode: ...
561
def normalize(self): ...
562
563
# Node comparison
564
def isSupported_version_(self, feature: str, version: str) -> bool: ...
565
def hasAttributes(self) -> bool: ...
566
def isSameNode_(self, other: DOMNode) -> bool: ...
567
def isEqualNode_(self, other: DOMNode) -> bool: ...
568
def compareDocumentPosition_(self, other: DOMNode) -> int: ...
569
def contains_(self, other: DOMNode) -> bool: ...
570
```
571
572
### Event System
573
574
#### DOMEvent
575
576
Base event class for all DOM events.
577
578
```python { .api }
579
class DOMEvent:
580
# Event phases
581
CAPTURING_PHASE = 1
582
AT_TARGET = 2
583
BUBBLING_PHASE = 3
584
585
@property
586
def type(self) -> str: ...
587
@property
588
def target(self) -> DOMEventTarget: ...
589
@property
590
def currentTarget(self) -> DOMEventTarget: ...
591
@property
592
def eventPhase(self) -> int: ...
593
def bubbles(self) -> bool: ...
594
def cancelable(self) -> bool: ...
595
@property
596
def timeStamp(self) -> int: ...
597
598
def stopPropagation(self): ...
599
def preventDefault(self): ...
600
def initEvent_canBubbleArg_cancelableArg_(self, eventTypeArg: str, canBubbleArg: bool, cancelableArg: bool): ...
601
602
# Legacy properties
603
def cancelBubble(self) -> bool: ...
604
def setCancelBubble_(self, cancelBubble: bool): ...
605
def returnValue(self) -> bool: ...
606
def setReturnValue_(self, returnValue: bool): ...
607
@property
608
def srcElement(self) -> DOMEventTarget: ...
609
```
610
611
#### DOMUIEvent
612
613
User interface event extending DOMEvent.
614
615
```python { .api }
616
class DOMUIEvent(DOMEvent):
617
@property
618
def view(self) -> DOMAbstractView: ...
619
@property
620
def detail(self) -> int: ...
621
@property
622
def which(self) -> int: ...
623
624
def initUIEvent_canBubble_cancelable_view_detail_(self, type: str, canBubble: bool, cancelable: bool, view: DOMAbstractView, detail: int): ...
625
```
626
627
#### DOMMouseEvent
628
629
Mouse event extending DOMUIEvent.
630
631
```python { .api }
632
class DOMMouseEvent(DOMUIEvent):
633
@property
634
def screenX(self) -> int: ...
635
@property
636
def screenY(self) -> int: ...
637
@property
638
def clientX(self) -> int: ...
639
@property
640
def clientY(self) -> int: ...
641
@property
642
def ctrlKey(self) -> bool: ...
643
@property
644
def shiftKey(self) -> bool: ...
645
@property
646
def altKey(self) -> bool: ...
647
@property
648
def metaKey(self) -> bool: ...
649
@property
650
def button(self) -> int: ...
651
@property
652
def relatedTarget(self) -> DOMEventTarget: ...
653
@property
654
def offsetX(self) -> int: ...
655
@property
656
def offsetY(self) -> int: ...
657
@property
658
def x(self) -> int: ...
659
@property
660
def y(self) -> int: ...
661
@property
662
def fromElement(self) -> DOMNode: ...
663
@property
664
def toElement(self) -> DOMNode: ...
665
666
def initMouseEvent_canBubble_cancelable_view_detail_screenX_screenY_clientX_clientY_ctrlKey_altKey_shiftKey_metaKey_button_relatedTarget_(self, type: str, canBubble: bool, cancelable: bool, view: DOMAbstractView, detail: int, screenX: int, screenY: int, clientX: int, clientY: int, ctrlKey: bool, altKey: bool, shiftKey: bool, metaKey: bool, button: int, relatedTarget: DOMEventTarget): ...
667
```
668
669
#### DOMKeyboardEvent
670
671
Keyboard event extending DOMUIEvent.
672
673
```python { .api }
674
class DOMKeyboardEvent(DOMUIEvent):
675
# Key locations
676
KEY_LOCATION_STANDARD = 0
677
KEY_LOCATION_LEFT = 1
678
KEY_LOCATION_RIGHT = 2
679
KEY_LOCATION_NUMPAD = 3
680
681
@property
682
def key(self) -> str: ...
683
@property
684
def code(self) -> str: ...
685
@property
686
def location(self) -> int: ...
687
@property
688
def ctrlKey(self) -> bool: ...
689
@property
690
def shiftKey(self) -> bool: ...
691
@property
692
def altKey(self) -> bool: ...
693
@property
694
def metaKey(self) -> bool: ...
695
@property
696
def repeat(self) -> bool: ...
697
@property
698
def isComposing(self) -> bool: ...
699
@property
700
def charCode(self) -> int: ...
701
@property
702
def keyCode(self) -> int: ...
703
@property
704
def which(self) -> int: ...
705
706
def getModifierState_(self, key: str) -> bool: ...
707
```
708
709
### CSS and Styling
710
711
#### DOMCSSStyleDeclaration
712
713
CSS style declaration for element styling.
714
715
```python { .api }
716
class DOMCSSStyleDeclaration:
717
@property
718
def cssText(self) -> str: ...
719
@property
720
def length(self) -> int: ...
721
@property
722
def parentRule(self) -> DOMCSSRule: ...
723
724
def getPropertyValue_(self, propertyName: str) -> str: ...
725
def getPropertyCSSValue_(self, propertyName: str) -> DOMCSSValue: ...
726
def removeProperty_(self, propertyName: str) -> str: ...
727
def getPropertyPriority_(self, propertyName: str) -> str: ...
728
def setProperty_value_priority_(self, propertyName: str, value: str, priority: str): ...
729
def item_(self, index: int) -> str: ...
730
def isPropertyImplicit_(self, propertyName: str) -> bool: ...
731
```
732
733
### Collections and Lists
734
735
#### DOMNodeList
736
737
List of DOM nodes.
738
739
```python { .api }
740
class DOMNodeList:
741
@property
742
def length(self) -> int: ...
743
744
def item_(self, index: int) -> DOMNode: ...
745
```
746
747
#### DOMHTMLCollection
748
749
Collection of HTML elements.
750
751
```python { .api }
752
class DOMHTMLCollection:
753
@property
754
def length(self) -> int: ...
755
756
def item_(self, index: int) -> DOMNode: ...
757
def namedItem_(self, name: str) -> DOMNode: ...
758
```
759
760
### Usage Examples
761
762
#### Finding and Modifying Elements
763
764
```python
765
import WebKit
766
767
# Assume you have a WebView instance
768
web_view = get_web_view() # Your WebView instance
769
document = web_view.mainFrame().DOMDocument()
770
771
# Find element by ID
772
element = document.getElementById_("myButton")
773
if element:
774
# Modify attributes
775
element.setAttribute_value_("class", "highlighted")
776
777
# Change content
778
element.setInnerHTML_("<strong>Click me!</strong>")
779
780
# Add event listener (in practice, you'd use JavaScript for this)
781
# This is just to show the API structure
782
print(f"Element tag: {element.tagName()}")
783
print(f"Element ID: {element.id()}")
784
785
# Find elements by tag name
786
inputs = document.getElementsByTagName_("input")
787
for i in range(inputs.length()):
788
input_element = inputs.item_(i)
789
if hasattr(input_element, 'type') and input_element.type() == "text":
790
input_element.setValue_("Default text")
791
```
792
793
#### Working with Forms
794
795
```python
796
import WebKit
797
798
document = web_view.mainFrame().DOMDocument()
799
800
# Find form
801
form = document.getElementById_("myForm")
802
803
# Get all input elements in the form
804
inputs = form.getElementsByTagName_("input")
805
806
# Process each input
807
for i in range(inputs.length()):
808
input_elem = inputs.item_(i)
809
810
if input_elem.type() == "text":
811
print(f"Text input value: {input_elem.value()}")
812
input_elem.setValue_("New value")
813
814
elif input_elem.type() == "checkbox":
815
print(f"Checkbox checked: {input_elem.checked()}")
816
input_elem.setChecked_(True)
817
818
# Work with select elements
819
selects = form.getElementsByTagName_("select")
820
if selects.length() > 0:
821
select_elem = selects.item_(0)
822
print(f"Selected index: {select_elem.selectedIndex()}")
823
select_elem.setSelectedIndex_(2) # Select third option
824
```
825
826
#### Document Manipulation
827
828
```python
829
import WebKit
830
831
document = web_view.mainFrame().DOMDocument()
832
833
# Create new elements
834
div = document.createElement_("div")
835
div.setAttribute_value_("id", "newDiv")
836
div.setAttribute_value_("class", "container")
837
838
# Create text content
839
text = document.createTextNode_("Hello, World!")
840
div.appendChild_(text)
841
842
# Add to document
843
body = document.body()
844
body.appendChild_(div)
845
846
# Execute document commands
847
document.execCommand_("selectAll")
848
print(f"Can cut: {document.queryCommandEnabled_('cut')}")
849
```