Skip to content

Commit 6130ec4

Browse files
author
kaliiiiiiiiii
committed
some refactoring
1 parent 481a024 commit 6130ec4

File tree

8 files changed

+119
-46
lines changed

8 files changed

+119
-46
lines changed

README.md

Lines changed: 97 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,118 @@
1-
# 🎭 [Playwright](https://playwright.dev) for Python [![PyPI version](https://badge.fury.io/py/playwright.svg)](https://pypi.python.org/pypi/playwright/) [![Anaconda version](https://img.shields.io/conda/v/microsoft/playwright)](https://anaconda.org/Microsoft/playwright) [![Join Discord](https://img.shields.io/badge/join-discord-infomational)](https://aka.ms/playwright/discord)
1+
# 🎭 [Playwright](https://playwright.dev) for Python
22

3-
Playwright is a Python library to automate [Chromium](https://www.chromium.org/Home), [Firefox](https://www.mozilla.org/en-US/firefox/new/) and [WebKit](https://webkit.org/) browsers with a single API. Playwright delivers automation that is **ever-green**, **capable**, **reliable** and **fast**. [See how Playwright is better](https://playwright.dev/python/docs/why-playwright).
3+
[![PyPI version](https://badge.fury.io/py/undetected-playwright-patch.svg)](https://badge.fury.io/py/undetected-playwright-patch)
44

5+
This is a patch of the original playwright implementation for Python.
56

6-
## Documentation
7+
It currently passes for sure (tested on Win10):
8+
-[CloudFare]
9+
-[Bet365] (shape//F5 I think)
10+
- [Others] Unknown/Not tested
711

8-
[https://playwright.dev/python/docs/intro](https://playwright.dev/python/docs/intro)
12+
Warnings:
13+
* the **Only chromium** part for Playwright is patched.
914

10-
## API Reference
15+
## Demos (tested on Win 10)
16+
![img.png](assets/nowsecure_nl.png)
17+
![img.png](assets/creep_js.png)
1118

12-
[https://playwright.dev/python/docs/api/class-playwright](https://playwright.dev/python/docs/api/class-playwright)
19+
20+
## Dependencies
21+
22+
* Google-Chrome installed (`channel="chrome"` recommended, default)
23+
24+
## Installation
25+
26+
#### From PyPi (recommended)
27+
28+
execute in your shell console
29+
```shell
30+
pip install undetected-playwright-patch
31+
```
32+
33+
#### Build from this repo:
34+
```
35+
git clone https://github.com/kaliiiiiiiiii/undetected-playwright-python
36+
cd undetected-playwright-python
37+
python -m pip install -r local-requirements.txt
38+
python build_patched.py
39+
```
1340

1441
## Example
1542

43+
```python
44+
import asyncio
45+
46+
# undetected-playwright here!
47+
from undetected_playwright.async_api import async_playwright, Playwright
48+
49+
50+
async def run(playwright: Playwright):
51+
args = []
52+
53+
# disable navigator.webdriver:true flag
54+
args.append("--disable-blink-features=AutomationControlled")
55+
browser = await playwright.chromium.launch(headless=False,
56+
args=args)
57+
page = await browser.new_page()
58+
await page.goto("https://nowsecure.nl/#relax")
59+
input("Press ENTER to continue to Creep-JS:")
60+
await page.goto("https://nowsecure.nl/#relax")
61+
await page.goto("https://abrahamjuliot.github.io/creepjs/")
62+
input("Press ENTER to exit:")
63+
await browser.close()
64+
65+
66+
async def main():
67+
async with async_playwright() as playwright:
68+
await run(playwright)
69+
70+
71+
if __name__ == "__main__":
72+
loop = asyncio.ProactorEventLoop()
73+
loop.run_until_complete(main())
74+
# asyncio.run(main) # should work for non-Windows as well
75+
```
76+
1677
```py
78+
79+
# undetected-playwright here!
1780
from undetected_playwright.sync_api import sync_playwright
1881

82+
1983
with sync_playwright() as p:
20-
for browser_type in [p.chromium, p.firefox, p.webkit]:
21-
browser = browser_type.launch()
22-
page = browser.new_page()
23-
page.goto('http://playwright.dev')
24-
page.screenshot(path=f'example-{browser_type.name}.png')
25-
browser.close()
84+
args = []
85+
86+
# disable navigator.webdriver:true flag
87+
args.append("--disable-blink-features=AutomationControlled")
88+
browser = p.chromium.launch(args=args, headless=False)
89+
page = browser.new_page()
90+
page.goto("https://nowsecure.nl/#relax")
91+
input("Press ENTER to continue to Creep-JS:")
92+
page.goto("https://nowsecure.nl/#relax")
93+
page.goto("https://abrahamjuliot.github.io/creepjs/")
94+
input("Press ENTER to exit:")
95+
browser.close()
2696
```
2797

28-
```py
29-
import asyncio
30-
from undetected_playwright.async_api import async_playwright
98+
## Documentation
3199

100+
See the original
101+
[https://playwright.dev/python/docs/intro](https://playwright.dev/python/docs/intro)
32102

33-
async def main():
34-
async with async_playwright() as p:
35-
for browser_type in [p.chromium, p.firefox, p.webkit]:
36-
browser = await browser_type.launch()
37-
page = await browser.new_page()
38-
await page.goto('http://playwright.dev')
39-
await page.screenshot(path=f'example-{browser_type.name}.png')
40-
await browser.close()
103+
## API Reference
104+
105+
[https://playwright.dev/python/docs/api/class-playwright](https://playwright.dev/python/docs/api/class-playwright)
41106

42107

43-
asyncio.run(main())
44-
```
45108

46-
## Other languages
109+
## Patches
110+
- [ ] [`Runtime.enable`](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-enable)
111+
- [x] remove Runtime.enable occurences
112+
- [x] patch _context(world) getter
113+
- [x] isolatedWorld (utility)
114+
- [ ] main world (main)
115+
- [x] reset on frame-reload//navigation
47116

48-
More comfortable in another programming language? [Playwright](https://playwright.dev) is also available in
49-
- [Node.js (JavaScript / TypeScript)](https://playwright.dev/docs/intro),
50-
- [.NET](https://playwright.dev/dotnet/docs/intro),
51-
- [Java](https://playwright.dev/java/docs/intro).
117+
## TODO's
118+
- [ ] add GitHub runner to build releases automated

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
async def run(playwright: Playwright):
88
args = []
99
args.append("--disable-blink-features=AutomationControlled")
10-
browser = await playwright.chromium.launch(channel="chrome", headless=False,
10+
browser = await playwright.chromium.launch(headless=False,
1111
args=args)
1212
page = await browser.new_page()
1313
await page.goto("https://nowsecure.nl/#relax")

main_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
with sync_playwright() as p:
66
args = []
77
args.append("--disable-blink-features=AutomationControlled")
8-
browser = p.chromium.launch(args=args, headless=False, channel="chrome")
8+
browser = p.chromium.launch(args=args, headless=False)
99
page = browser.new_page()
1010
page.goto("https://nowsecure.nl/#relax")
1111
input("Press ENTER to continue to Creep-JS:")

patch_check.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
async def run(playwright: Playwright):
66
args = []
77
args.append("--disable-blink-features=AutomationControlled")
8-
browser = await playwright.chromium.launch(channel="chrome", headless=False,
9-
ignore_default_args=['--enable-automation'],
10-
args=args)
8+
browser = await playwright.chromium.launch( headless=False, args=args)
119
page = await browser.new_page()
1210
await page.goto("https://hmaker.github.io/selenium-detector/")
1311
await page.evaluate("document")

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[metadata]
2-
version = attr: playwright.__version__
2+
version = attr: undetected_playwright.__version__
33

44
[flake8]
55
ignore =

setup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def extractall(zip: zipfile.ZipFile, path: str) -> None:
4444

4545

4646
def download_driver(zip_name: str) -> None:
47-
zip_file = f"undetected_playwright-{driver_version}-{zip_name}.zip"
47+
zip_file = f"playwright-{driver_version}-{zip_name}.zip"
4848
if os.path.exists("driver/" + zip_file):
4949
return
5050
url = "https://playwright.azureedge.net/builds/driver/"
@@ -144,7 +144,7 @@ def _build_wheels(
144144
for wheel_bundle in wheels:
145145
download_driver(wheel_bundle["zip_name"])
146146
zip_file = (
147-
f"driver/undetected_playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
147+
f"driver/playwright-{driver_version}-{wheel_bundle['zip_name']}.zip"
148148
)
149149
with zipfile.ZipFile(zip_file, "r") as zip:
150150
extractall(zip, f"driver/{wheel_bundle['zip_name']}")
@@ -196,17 +196,17 @@ def _download_and_extract_local_driver(
196196
assert len(zip_names_for_current_system) == 1
197197
zip_name = zip_names_for_current_system.pop()
198198
download_driver(zip_name)
199-
zip_file = f"driver/undetected_playwright-{driver_version}-{zip_name}.zip"
199+
zip_file = f"driver/playwright-{driver_version}-{zip_name}.zip"
200200
with zipfile.ZipFile(zip_file, "r") as zip:
201201
extractall(zip, "undetected_playwright/driver")
202202
patch_driver(os.getcwd() + "/undetected_playwright/driver")
203203

204204

205205
setup(
206-
name="undetected-undetected_playwright-patch",
206+
name="undetected-playwright-patch",
207207
author="Microsoft Corporation, patches by github.com/kaliiiiiiiiii",
208208
author_email="",
209-
description="Undetected version of undetected_playwright",
209+
description="Undetected version of playwright",
210210
long_description=Path("README.md").read_text(encoding="utf-8"),
211211
long_description_content_type="text/markdown",
212212
license="Apache-2.0",

undetected_playwright/_impl/_browser_type.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def executable_path(self) -> str:
7171
async def launch(
7272
self,
7373
executablePath: Union[str, Path] = None,
74-
channel: str = None,
74+
channel: str = "chrome",
7575
args: Sequence[str] = None,
7676
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
7777
handleSIGINT: bool = None,
@@ -90,7 +90,8 @@ async def launch(
9090
) -> Browser:
9191

9292
# undetected-undetected_playwright patches
93-
if not ignoreDefaultArgs:
93+
if ignoreDefaultArgs is None:
94+
# noinspection PyPep8Naming
9495
ignoreDefaultArgs = []
9596
ignoreDefaultArgs.append('--enable-automation')
9697

@@ -105,7 +106,7 @@ async def launch(
105106
async def launch_persistent_context(
106107
self,
107108
userDataDir: Union[str, Path],
108-
channel: str = None,
109+
channel: str = "chrome",
109110
executablePath: Union[str, Path] = None,
110111
args: Sequence[str] = None,
111112
ignoreDefaultArgs: Union[bool, Sequence[str]] = None,
@@ -154,6 +155,13 @@ async def launch_persistent_context(
154155
recordHarMode: HarMode = None,
155156
recordHarContent: HarContentPolicy = None,
156157
) -> BrowserContext:
158+
159+
# undetected-undetected_playwright patches
160+
if ignoreDefaultArgs is None:
161+
# noinspection PyPep8Naming
162+
ignoreDefaultArgs = []
163+
ignoreDefaultArgs.append('--enable-automation')
164+
157165
userDataDir = str(Path(userDataDir)) if userDataDir else ""
158166
params = locals_to_params(locals())
159167
await prepare_browser_context_params(params)

undetected_playwright/_impl/_driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def compute_driver_executable() -> Path:
2525
package_path = Path(inspect.getfile(undetected_playwright)).parent
2626
platform = sys.platform
2727
if platform == "win32":
28-
return package_path / "driver" / "undetected_playwright.cmd"
29-
return package_path / "driver" / "undetected_playwright.sh"
28+
return package_path / "driver" / "playwright.cmd"
29+
return package_path / "driver" / "playwright.sh"
3030

3131

3232
def get_driver_env() -> dict:

0 commit comments

Comments
 (0)