undetected-chromedriver/example/test_workflow.py

102 lines
2.8 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 18:30:50 -06:00
2023-06-02 17:40:27 -06:00
def main():
2023-06-02 18:30:50 -06:00
####
# this block is a dirty helper since
# in the action runner devices serveral chrome versions exists
# and i need to ensure it takes the one which is installed
# by the task.
####
2023-06-02 17:40:27 -06:00
for k,v in os.environ.items():
logger.info("%s = %s" % (k,v))
logger.info('==== END ENV ==== ')
tmp = Path('/tmp').resolve()
2023-06-02 18:30:50 -06:00
2023-06-02 17:40:27 -06:00
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 18:30:50 -06:00
####
# test really starts here
#3##
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 18:39:06 -06:00
logging.getLogger().setLevel(10)
2023-06-02 17:40:27 -06:00
driver.get('https://www.nowsecure.nl')
2023-06-02 18:30:50 -06:00
logger.info('current url %s' % driver.current_url)
2023-06-02 17:40:27 -06:00
try:
2023-06-02 18:39:06 -06:00
WebDriverWait(driver,15).until(EC.title_contains('moment'))
2023-06-02 17:40:27 -06:00
except TimeoutException:
pass
2023-06-02 18:30:50 -06:00
2023-06-02 18:39:06 -06:00
logger.info('current page source:\n%s' % driver.page_source)
2023-06-02 18:30:50 -06:00
logger.info('current url %s' % driver.current_url)
2023-06-02 17:40:27 -06:00
try:
2023-06-02 18:39:06 -06:00
WebDriverWait(driver,15).until(EC.title_contains('nowSecure'))
logger.info('PASSED CLOUDFLARE!')
except TimeoutException:
logger.info('timeout')
2023-06-02 17:40:27 -06:00
print(driver.current_url)
2023-06-02 18:30:50 -06:00
2023-06-02 18:51:05 -06:00
logger.info('current page source:\n%s\n' % driver.page_source)
2023-06-02 18:30:50 -06:00
logger.info('trying to save a screenshot via imgur')
2023-06-02 18:51:05 -06:00
driver.save_screenshot('/home/runner/work/_temp/screenshot.png')
2023-06-02 18:30:50 -06:00
driver.get('https://imgur.com/upload')
2023-06-02 18:51:05 -06:00
driver.find_element('css selector', 'input').send_keys('/home/runner/work/_temp/screenshot.png')
2023-06-02 18:30:50 -06:00
time.sleep(1)
logger.info('current url %s' % driver.current_url)
time.sleep(1)
2023-06-02 18:51:05 -06:00
logger.info(f'A SCREENSHOT IS SAVED ON {driver.current_url} <<< if this ends onlywith /upload than it failed. after all we are running from a datacenter no human being would ever surf the internet from ')
2023-06-02 18:30:50 -06:00
time.sleep(5)
2023-06-02 18:51:05 -06:00
2023-06-02 17:40:27 -06:00
driver.quit()
2023-06-02 18:30:50 -06:00
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()