tab
logger = logging.getLogger(__name__)
module-attribute
Tab
Bases: Connection
:ref:tab is the controlling mechanism/connection to a 'target',
for most of us 'target' can be read as 'tab'. however it could also
be an iframe, serviceworker or background script for example,
although there isn't much to control for those.
if you open a new window by using :py:meth:browser.get(..., new_window=True)
your url will open a new window. this window is a 'tab'.
When you browse to another page, the tab will be the same (it is an browser view).
So it's important to keep some reference to tab objects, in case you're done interacting with elements and want to operate on the page level again.
Custom CDP commands
Tab object provide many useful and often-used methods. It is also possible to utilize the included cdp classes to to something totally custom.
the cdp package is a set of so-called "domains" with each having methods, events and types.
to send a cdp method, for example :py:obj:cdp.page.navigate, you'll have to check
whether the method accepts any parameters and whether they are required or not.
you can use
so tab.send() accepts a generator object, which is created by calling a cdp method. this way you can build very detailed and customized commands. (note: finding correct command combo's can be a time consuming task, luckily i added a whole bunch of useful methods, preferably having the same api's or lookalikes, as in selenium)
some useful, often needed and simply required methods
:py:meth:~find | find(text)
find and returns a single element by text match. by default returns the first element found.
much more powerful is the best_match flag, although also much more expensive.
when no match is found, it will retry for
:py:meth:~find | find(text, best_match=True) or find(text, True)
Much more powerful (and expensive!!) than the above, is the use of the find(text, best_match=True) flag.
It will still return 1 element, but when multiple matches are found, picks the one having the
most similar text length.
How would that help?
For example, you search for "login", you'd probably want the "login" button element,
and not thousands of scripts,meta,headings which happens to contain a string of "login".
when no match is found, it will retry for
:py:meth:~select | select(selector)
find and returns a single element by css selector match.
when no match is found, it will retry for
:py:meth:~select_all | select_all(selector)
find and returns all elements by css selector match.
when no match is found, it will retry for
await :py:obj:Tab
calling await tab will do a lot of stuff under the hood, and ensures all references
are up to date. also it allows for the script to "breathe", as it is oftentime faster than your browser or
webpage. So whenever you get stuck and things crashes or element could not be found, you should probably let
it "breathe" by calling await page and/or await page.sleep()
also, it's ensuring :py:obj:~url will be updated to the most recent one, which is quite important in some
other methods.
Using other and custom CDP commands
using the included cdp module, you can easily craft commands, which will always return an generator object.
this generator object can be easily sent to the :py:meth:~send method.
:py:meth:~send
this is probably THE most important method, although you won't ever call it, unless you want to
go really custom. the send method accepts a :py:obj:cdp command. Each of which can be found in the
cdp section.
when you import * from this package, cdp will be in your namespace, and contains all domains/actions/events you can act upon.
Source code in zendriver/core/tab.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 | |
browser: Browser | None = browser
instance-attribute
inspector_url: str
property
get the inspector url. this url can be used in another browser to show you the devtools interface for current tab. useful for debugging (and headless)
Returns:
| Type | Description |
|---|---|
|
|
__call__(text=None, selector=None, timeout=10)
async
alias to query_selector_all or find_elements_by_text, depending on whether text= is set or selector= is set
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str | None
|
css selector string |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
__eq__(other)
__init__(websocket_url, target, browser=None, **kwargs)
Source code in zendriver/core/tab.py
__repr__()
activate()
async
active this target (ie: tab,window,page)
back()
async
bring_to_front()
async
close()
async
close the current target (ie: tab,window,page)
Returns:
| Type | Description |
|---|---|
|
|
Raises:
| Type | Description |
|---|---|
asyncio.TimeoutError
|
|
RuntimeError
|
|
Source code in zendriver/core/tab.py
disable_dom_agent()
async
Source code in zendriver/core/tab.py
download_file(url, filename=None)
async
downloads file by given url.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
url of the file |
required |
filename
|
Optional[PathLike]
|
the name for the file. if not specified the name is composed from the url file name |
None
|
Source code in zendriver/core/tab.py
evaluate(expression, await_promise=False, return_by_value=True)
async
Source code in zendriver/core/tab.py
expect_download()
Creates a download expectation for next download.
Returns:
| Type | Description |
|---|---|
DownloadExpectation
|
A DownloadExpectation instance. |
expect_request(url_pattern)
Creates a request expectation for a specific URL pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_pattern
|
Union[str, Pattern[str]]
|
The URL pattern to match requests. |
required |
Returns:
| Type | Description |
|---|---|
RequestExpectation
|
A RequestExpectation instance. |
Source code in zendriver/core/tab.py
expect_response(url_pattern)
Creates a response expectation for a specific URL pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_pattern
|
Union[str, Pattern[str]]
|
The URL pattern to match responses. |
required |
Returns:
| Type | Description |
|---|---|
ResponseExpectation
|
A ResponseExpectation instance. |
Source code in zendriver/core/tab.py
find(text, best_match=True, return_enclosing_element=True, timeout=10)
async
find single element by text can also be used to wait for such element to appear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
text to search for. note: script contents are also considered text |
required |
best_match
|
bool
|
when True (default), it will return the element which has the most comparable string length. this could help tremendously, when for example you search for "login", you'd probably want the login button element, and not thousands of scripts,meta,headings containing a string of "login". When False, it will return naively just the first match (but is way faster). |
True
|
return_enclosing_element
|
bool
|
since we deal with nodes instead of elements, the find function most often returns so called text nodes, which is actually a element of plain text, which is the somehow imaginary "child" of a "span", "p", "script" or any other elements which have text between their opening and closing tags. most often when we search by text, we actually aim for the element containing the text instead of a lousy plain text node, so by default the containing element is returned. however, there are (why not) exceptions, for example elements that use the "placeholder=" property. this text is rendered, but is not a pure text node. in that case you can set this flag to False. since in this case we are probably interested in just that element, and not it's parent. # todo, automatically determine node type # ignore the return_enclosing_element flag if the found node is NOT a text node but a # regular element (one having a tag) in which case that is exactly what we need. |
True
|
timeout
|
Union[int, float]
|
raise timeout exception when after this many seconds nothing is found. |
10
|
Source code in zendriver/core/tab.py
find_all(text, timeout=10)
async
find multiple elements by text can also be used to wait for such element to appear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
text to search for. note: script contents are also considered text |
required |
timeout
|
Union[int, float]
|
raise timeout exception when after this many seconds nothing is found. |
10
|
Source code in zendriver/core/tab.py
find_element_by_text(text, best_match=False, return_enclosing_element=True)
async
finds and returns the first element containing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
|
required |
best_match
|
Optional[bool]
|
when True, which is MUCH more expensive (thus much slower), will find the closest match based on length. this could help tremendously, when for example you search for "login", you'd probably want the login button element, and not thousands of scripts,meta,headings containing a string of "login". |
False
|
return_enclosing_element
|
Optional[bool]
|
|
True
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
find_elements_by_text(text, tag_hint=None)
async
returns element which match the given text. please note: this may (or will) also return any other element (like inline scripts), which happen to contain that text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
|
required |
tag_hint
|
Optional[str]
|
when provided, narrows down search to only elements which match given tag eg: a, div, script, span |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | |
flash_point(x, y, duration=0.5, size=10)
async
Source code in zendriver/core/tab.py
forward()
async
fullscreen()
async
get(url='about:blank', new_tab=False, new_window=False)
async
top level get. utilizes the first tab to retrieve given url.
convenience function known from selenium. this function handles waits/sleeps and detects when DOM events fired, so it's the safest way of navigating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
the url to navigate to |
'about:blank'
|
new_tab
|
bool
|
open new tab |
False
|
new_window
|
bool
|
open new window |
False
|
Returns:
| Type | Description |
|---|---|
Tab
|
Page |
Source code in zendriver/core/tab.py
get_all_linked_sources()
async
get all elements of tag: link, a, img, scripts meta, video, audio
Returns:
| Type | Description |
|---|---|
List[Element]
|
|
Source code in zendriver/core/tab.py
get_all_urls(absolute=True)
async
convenience function, which returns all links (a,link,img,script,meta)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
absolute
|
bool
|
try to build all the links in absolute form instead of "as is", often relative |
True
|
Returns:
| Type | Description |
|---|---|
List[str]
|
list of urls |
Source code in zendriver/core/tab.py
get_content()
async
gets the current page source content (html)
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
get_local_storage()
async
get local storage items as dict of strings (careful!, proper deserialization needs to be done if needed)
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
get_window()
async
get the window Bounds
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
inspector_open()
intercept(url_pattern, request_stage, resource_type)
Sets up interception for network requests matching a URL pattern, request stage, and resource type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_pattern
|
str
|
URL string or regex pattern to match requests. |
required |
request_stage
|
RequestStage
|
Stage of the request to intercept (e.g., request, response). |
required |
resource_type
|
ResourceType
|
Type of resource (e.g., Document, Script, Image). |
required |
Returns:
| Type | Description |
|---|---|
BaseFetchInterception Use this to block, modify, | inspect network traffic for specific resources during browser automation.
|
A BaseFetchInterception instance for further configuration or awaiting intercepted requests. |
Source code in zendriver/core/tab.py
js_dumps(obj_name, return_by_value=True)
async
dump given js object with its properties and values as a dict
note: complex objects might not be serializable, therefore this method is not a "source of thruth"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj_name
|
str
|
the js object to dump |
required |
return_by_value
|
Optional[bool]
|
if you want an tuple of cdp objects (returnvalue, errors), set this to False example ------ x = await self.js_dumps('window') print(x) '...{ 'pageYOffset': 0, 'visualViewport': {}, 'screenX': 10, 'screenY': 10, 'outerWidth': 1050, 'outerHeight': 832, 'devicePixelRatio': 1, 'screenLeft': 10, 'screenTop': 10, 'styleMedia': {}, 'onsearch': None, 'isSecureContext': True, 'trustedTypes': {}, 'performance': {'timeOrigin': 1707823094767.9, 'timing': {'connectStart': 0, 'navigationStart': 1707823094768, ]... ' |
True
|
Source code in zendriver/core/tab.py
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | |
maximize()
async
medimize()
async
minimize()
async
mouse_click(x, y, button='left', buttons=1, modifiers=0, _until_event=None, flash=False)
async
native click on position x,y
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
float
|
|
required |
x
|
float
|
|
required |
button
|
str
|
str (default = "left") |
'left'
|
buttons
|
Optional[int]
|
which button (default 1 = left) |
1
|
modifiers
|
Optional[int]
|
(Optional) Bit field representing pressed modifier keys. Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0). |
0
|
_until_event
|
Optional[type]
|
internal. event to wait for before returning |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in zendriver/core/tab.py
mouse_move(x, y, steps=10, flash=False)
async
Source code in zendriver/core/tab.py
open_external_inspector()
async
opens the system's browser containing the devtools inspector page for this tab. could be handy, especially to debug in headless mode.
Source code in zendriver/core/tab.py
print_to_pdf(filename, **kwargs)
async
Prints the current page to a PDF file and saves it to the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
PathLike
|
The path where the PDF will be saved. |
required |
kwargs
|
Any
|
Additional options for printing to be passed to :py:obj: |
{}
|
Returns:
| Type | Description |
|---|---|
pathlib.Path
|
The path to the saved PDF file. |
Source code in zendriver/core/tab.py
query_selector(selector, _node=None)
async
find single element based on css selector string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
css selector(s) |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
query_selector_all(selector, _node=None)
async
equivalent of javascripts document.querySelectorAll. this is considered one of the main methods to use in this package.
it returns all matching :py:obj:zendriver.Element objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
css selector. (first time? => https://www.w3schools.com/cssref/css_selectors.php ) |
required |
_node
|
Node | Element | None
|
internal use |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
reload(ignore_cache=True, script_to_evaluate_on_load=None)
async
Reloads the page
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ignore_cache
|
Optional[bool]
|
when set to True (default), it ignores cache, and re-downloads the items |
True
|
script_to_evaluate_on_load
|
Optional[str]
|
script to run on load. I actually haven't experimented with this one, so no guarantees. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
save_screenshot(filename='auto', format='jpeg', full_page=False)
async
Saves a screenshot of the page.
This is not the same as :py:obj:Element.save_screenshot, which saves a screenshot of a single element only
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Optional[PathLike]
|
uses this as the save path |
'auto'
|
format
|
str
|
jpeg or png (defaults to jpeg) |
'jpeg'
|
full_page
|
bool
|
when False (default) it captures the current viewport. when True, it captures the entire page |
False
|
Returns:
| Type | Description |
|---|---|
str
|
the path/filename of saved screenshot |
Source code in zendriver/core/tab.py
save_snapshot(filename='snapshot.mhtml')
async
Saves a snapshot of the page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The save path; defaults to "snapshot.mhtml" |
'snapshot.mhtml'
|
Source code in zendriver/core/tab.py
screenshot_b64(format='jpeg', full_page=False)
async
Takes a screenshot of the page and return the result as a base64 encoded string.
This is not the same as :py:obj:Element.screenshot_b64, which takes a screenshot of a single element only
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
jpeg or png (defaults to jpeg) |
'jpeg'
|
full_page
|
bool
|
when False (default) it captures the current viewport. when True, it captures the entire page |
False
|
Returns:
| Type | Description |
|---|---|
str
|
screenshot data as base64 encoded |
Source code in zendriver/core/tab.py
scroll_down(amount=25, speed=800)
async
scrolls down maybe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
int
|
number in percentage. 25 is a quarter of page, 50 half, and 1000 is 10x the page |
25
|
speed
|
int
|
number swipe speed in pixels per second (default: 800). |
800
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
scroll_up(amount=25, speed=800)
async
scrolls up maybe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
int
|
number in percentage. 25 is a quarter of page, 50 half, and 1000 is 10x the page |
25
|
speed
|
int
|
number swipe speed in pixels per second (default: 800). |
800
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
select(selector, timeout=10)
async
find single element by css selector. can also be used to wait for such element to appear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
css selector, eg a[href], button[class*=close], a > img[src] |
required |
timeout
|
Union[int, float]
|
raise timeout exception when after this many seconds nothing is found. |
10
|
Source code in zendriver/core/tab.py
select_all(selector, timeout=10, include_frames=False)
async
find multiple elements by css selector. can also be used to wait for such element to appear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
css selector, eg a[href], button[class*=close], a > img[src] |
required |
timeout
|
Union[int, float]
|
raise timeout exception when after this many seconds nothing is found. |
10
|
include_frames
|
bool
|
whether to include results in iframes. |
False
|
Source code in zendriver/core/tab.py
set_download_path(path)
async
sets the download path and allows downloads this is required for any download function to work (well not entirely, since when unset we set a default folder)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
|
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
set_local_storage(items)
async
set local storage. dict items must be strings. simple types will be converted to strings automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
dict[str, str]
|
dict containing {key:str, value:str} |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
set_user_agent(user_agent=None, accept_language=None, platform=None)
async
Set the user agent, accept language, and platform.
These correspond to: - navigator.userAgent - navigator.language - navigator.platform
Note: In most cases, you should instead pass the user_agent option to zendriver.start(). This ensures that the user agent is set before the browser starts and correctly applies to all pages and requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_agent
|
str | None
|
user agent string |
None
|
accept_language
|
str | None
|
accept language string |
None
|
platform
|
str | None
|
platform string |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
set_window_size(left=0, top=0, width=1280, height=1024)
async
set window size and position
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
int
|
pixels from the left of the screen to the window top-left corner |
0
|
top
|
int
|
pixels from the top of the screen to the window top-left corner |
0
|
width
|
int
|
width of the window in pixels |
1280
|
height
|
int
|
height of the window in pixels |
1024
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in zendriver/core/tab.py
set_window_state(left=0, top=0, width=1280, height=720, state='normal')
async
sets the window size or state.
for state you can provide the full name like minimized, maximized, normal, fullscreen, or something which leads to either of those, like min, mini, mi, max, ma, maxi, full, fu, no, nor in case state is set other than "normal", the left, top, width, and height are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
int
|
desired offset from left, in pixels |
0
|
top
|
int
|
desired offset from the top, in pixels |
0
|
width
|
int
|
desired width in pixels |
1280
|
height
|
int
|
desired height in pixels |
720
|
state
|
str
|
can be one of the following strings: - normal - fullscreen - maximized - minimized |
'normal'
|
Source code in zendriver/core/tab.py
verify_cf(click_delay=5, timeout=15, challenge_selector=None, flash_corners=False)
async
Finds and solves the Cloudflare checkbox challenge.
The total time for finding and clicking is governed by timeout.
Args: click_delay: The delay in seconds between clicks. timeout: The total time in seconds to wait for the challenge and solve it. challenge_selector: An optional CSS selector for the challenge input element. flash_corners: If True, flash the corners of the challenge element.
Raises: TimeoutError: If the checkbox is not found or solved within the timeout.
Source code in zendriver/core/tab.py
wait_for(selector=None, text=None, timeout=10)
async
variant on query_selector_all and find_elements_by_text this variant takes either selector or text, and will block until the requested element(s) are found.
it will block for a maximum of
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str | None
|
css selector |
None
|
text
|
str | None
|
text |
None
|
timeout
|
int | float
|
|
10
|
Returns:
| Type | Description |
|---|---|
Element
|
|
Raises:
| Type | Description |
|---|---|
asyncio.TimeoutError
|
|
Source code in zendriver/core/tab.py
wait_for_ready_state(until='interactive', timeout=10)
async
Waits for the page to reach a certain ready state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
until
|
Literal['loading', 'interactive', 'complete']
|
The ready state to wait for. Can be one of "loading", "interactive", or "complete". |
'interactive'
|
timeout
|
int
|
The maximum number of seconds to wait. |
10
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the ready state is reached. |
Raises:
| Type | Description |
|---|---|
asyncio.TimeoutError
|
If the timeout is reached before the ready state is reached. |
Source code in zendriver/core/tab.py
xpath(xpath, timeout=2.5)
async
find elements by xpath string.
if not immediately found, retries are attempted until :ref:timeout is reached (default 2.5 seconds).
in case nothing is found, it returns an empty list. It will not raise.
this timeout mechanism helps when relying on some element to appear before continuing your script.
.. code-block:: python
# find all the inline scripts (script elements without src attribute)
await tab.xpath('//script[not(@src)]')
# or here, more complex, but my personal favorite to case-insensitive text search
await tab.xpath('//text()[ contains( translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"),"test")]')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xpath
|
str
|
|
required |
timeout
|
float
|
2.5 |
2.5
|
Returns:
| Type | Description |
|---|---|
|
List[Element] or [] |