Skip to content

Commit

Permalink
Merge pull request #7 from pstemporowski/patch-1
Browse files Browse the repository at this point in the history
Improved the quality of the example code
  • Loading branch information
Mattwmaster58 authored Nov 19, 2024
2 parents c115046 + a501ea3 commit 27be1d6
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,68 @@ $ pip install git+https://github.com/Mattwmaster58/playwright_stealth
## Example Usage
### Recommended Usage
```python
import asyncio

from playwright.async_api import async_playwright
from playwright_stealth import Stealth, ALL_EVASIONS_DISABLED_KWARGS

from playwright_stealth import Stealth

async def main():
# This is the recommended usage. All pages created will have stealth applied:
async with Stealth().use_async(async_playwright()) as p:
# or, to hook every browser launched from this context: stealth.hook_playwright_context(p)
browser = await p.chromium.launch()
page = await browser.new_page()
print("from new_page: ", await page.evaluate("navigator.webdriver"))

webdriver_status = await page.evaluate("navigator.webdriver")
print("from new_page: ", webdriver_status)

different_context = await browser.new_context()
page_from_different_context = await different_context.new_page()
print("from new_context: ", await page_from_different_context.evaluate("navigator.webdriver"))

# Specifying config options and applying evasions manually to an entire context:
different_context_status = await page_from_different_context.evaluate("navigator.webdriver")
print("from new_context: ", different_context_status)

asyncio.run(main())
```
### Specifying config options and applying evasions manually to an entire context
```python
import asyncio
from playwright.async_api import async_playwright
from playwright_stealth import Stealth, ALL_EVASIONS_DISABLED_KWARGS

async def advanced_example():
# Custom configuration with specific languages
custom_languages = ("fr-FR", "fr")
stealth = Stealth(
navigator_languages_override=custom_languages,
init_scripts_only=True
)

async with async_playwright() as p:
browser = await p.chromium.launch()
# this isn't recommended, certain evasions will work worse, and some won't be able to be applied at all
context = await browser.new_context()
await stealth.apply_stealth_async(context)

# Test stealth on multiple pages
page_1 = await context.new_page()
concurrency_on_page_1_mocked = await page_1.evaluate("navigator.languages") == custom_languages
print("manually applied stealth applied to page 1:", concurrency_on_page_1_mocked)
page_2 = await context.new_page()
concurrency_on_page_2_mocked = await page_2.evaluate("navigator.languages") == custom_languages
print("manually applied stealth applied to page 2:", concurrency_on_page_2_mocked)

# a constant "ALL_EVASIONS_DISABLED_KWARGS" is provided if only a few evasions are desired:
assert len(Stealth(**ALL_EVASIONS_DISABLED_KWARGS).script_payload) == 0
# all but navigator_webdriver disabled
assert len(Stealth(**{**ALL_EVASIONS_DISABLED_KWARGS, "navigator_webdriver": True}).script_payload) > 0


asyncio.run(main())

# Verify language settings
for i, page in enumerate([page_1, page_2], 1):
is_mocked = await page.evaluate("navigator.languages") == custom_languages
print(f"Stealth applied to page {i}: {is_mocked}")

# Example of selective evasion usage
no_evasions = Stealth(**ALL_EVASIONS_DISABLED_KWARGS)
single_evasion = Stealth(**{**ALL_EVASIONS_DISABLED_KWARGS, "navigator_webdriver": True})

print("Total evasions (none):", len(no_evasions.script_payload))
print("Total evasions (single):", len(single_evasion.script_payload))

asyncio.run(advanced_example())
```

## Todo
- make this work with playwright.launch_persistent_context

Expand Down

0 comments on commit 27be1d6

Please sign in to comment.