undetected-chromedriver/example/test_workflow.py

74 lines
2.2 KiB
Python
Raw Normal View History

2023-06-02 17:31:47 -06:00
# coding: utf-8
2023-06-02 15:07:37 -06:00
2023-06-02 17:31:47 -06:00
import time
import logging
2023-06-02 17:34:28 -06:00
import os
2023-06-02 15:07:37 -06:00
from selenium.webdriver.support.wait import WebDriverWait
2023-06-02 17:31:47 -06:00
import selenium.webdriver.support.expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import undetected_chromedriver as uc
2023-06-02 17:34:28 -06:00
from pathlib import Path
2023-06-02 15:07:37 -06:00
2023-06-02 17:47:52 -06:00
logging.basicConfig(level=10)
2023-06-02 17:31:47 -06:00
logger = logging.getLogger('test')
2023-06-02 16:06:25 -06:00
2023-06-02 17:40:27 -06:00
def main():
for k,v in os.environ.items():
logger.info("%s = %s" % (k,v))
logger.info('==== END ENV ==== ')
tmp = Path('/tmp').resolve()
for item in tmp.rglob('**'):
logger.info('found %s ' % item)
2023-06-02 16:40:33 -06:00
2023-06-02 17:40:27 -06:00
if item.is_dir():
if 'chrome-' in item.name:
2023-06-02 18:01:52 -06:00
2023-06-02 17:53:42 -06:00
logger.info('adding %s to PATH' % str(item))
2023-06-02 17:54:59 -06:00
logger.info('current PATH: %s' % str(os.environ.get('PATH')))
2023-06-02 17:40:27 -06:00
path_list = os.environ['PATH'].split(os.pathsep)
path_list.insert(0, str(item))
os.environ['PATH'] = os.pathsep.join(path_list)
2023-06-02 17:54:59 -06:00
logger.info('new PATH %s:' % str(os.environ.get('PATH')))
2023-06-02 18:01:52 -06:00
browser_executable_path = str(item / 'chrome')
2023-06-02 17:40:27 -06:00
break
2023-06-02 17:31:47 -06:00
2023-06-02 17:47:52 -06:00
2023-06-02 18:01:52 -06:00
driver = uc.Chrome(headless=True, browser_executable_path=browser_executable_path)
2023-06-02 17:47:52 -06:00
logging.getLogger().setLevel(20)
2023-06-02 17:40:27 -06:00
driver.get('https://www.nowsecure.nl')
2023-06-02 17:31:47 -06:00
2023-06-02 17:40:27 -06:00
print(driver.current_url)
2023-06-02 17:31:47 -06:00
2023-06-02 17:40:27 -06:00
try:
WebDriverWait(driver,10).until(EC.visibility_of_element_located(("css selector", "body")))
except TimeoutException:
pass
2023-06-02 17:31:47 -06:00
print(driver.current_url)
2023-06-02 17:40:27 -06:00
try:
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element(("css selector", "main h1"), "OH YEAH, you passed!"))
2023-06-02 18:06:49 -06:00
except TimeoutException:
2023-06-02 17:40:27 -06:00
logging.getLogger().setLevel(20)
2023-06-02 17:43:50 -06:00
driver.reconnect()
2023-06-02 17:40:27 -06:00
print(driver.current_url)
logger.info('trying to save a screenshot via imgur')
# driver.reconnect()
2023-06-02 18:10:26 -06:00
driver.save_screenshot('/tmp/screenshot.png')
2023-06-02 17:40:27 -06:00
driver.get('https://imgur.com/upload')
2023-06-02 18:10:26 -06:00
2023-06-02 17:40:27 -06:00
driver.find_element('css selector', 'input').send_keys('/tmp/screenshot.jpg')
2023-06-02 18:10:26 -06:00
time.sleep(5)
2023-06-02 17:40:27 -06:00
logger.info('A SCREENSHOT IS SAVED ON %s' % driver.current_url)
time.sleep(5)
driver.quit()
2023-06-02 17:31:47 -06:00
2023-06-02 15:07:37 -06:00
2023-06-02 17:40:27 -06:00
if __name__ == "__main__":
main()