Open Kinuseka opened 1 year ago
For the time-being, there's a Python framework with automatic browser/driver detection that has undetected-chromedriver
integrated within it: SeleniumBase.
pip install seleniumbase
Then run the following script with python
:
import time
from seleniumbase import page_actions
from seleniumbase import DriverContext
with DriverContext(uc=True, incognito=True) as driver:
driver.get("https://nowsecure.nl/#relax")
page_actions.wait_for_text(
driver, "OH YEAH, you passed!", "h1", by="css selector"
)
print("\n Success! Website did not detect Selenium!")
time.sleep(2)
screenshot_name = "now_secure_image.png"
driver.save_screenshot(screenshot_name)
print("\nScreenshot saved to: %s" % screenshot_name)
The above example has DriverContext
as a Python context manager. The browser will quit automatically after the program leaves the with
block.
Or, if you prefer using pytest
, there's the BaseCase
format:
from seleniumbase import BaseCase
if __name__ == "__main__":
from pytest import main
main([__file__, "--uc", "--incognito", "-s"])
class UndetectedTest(BaseCase):
def test_browser_is_undetected(self):
if not self.undetectable or not self.incognito:
self.get_new_driver(undetectable=True, incognito=True)
self.open("https://nowsecure.nl/#relax")
try:
self.assert_text("OH YEAH, you passed!", "h1", timeout=6.75)
self.post_message("Selenium wasn't detected!", duration=2.8)
self._print("\n Success! Website did not detect Selenium! ")
self.save_screenshot_to_logs()
except Exception:
self.fail('Selenium was detected! Try using: "pytest --uc"')
what if I get rejected before even querying?
Making the program automatically determine the current version of the installed chromium on the current environment to install the correct version of chromedriver, instead of downloading to the latest version.