-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeleniumAction.py
More file actions
64 lines (48 loc) · 2.14 KB
/
SeleniumAction.py
File metadata and controls
64 lines (48 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import os
import sys
import time
from datetime import datetime
from pathlib import Path
from random import random
from typing import List
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import undetected_chromedriver as uc
from SeleniumDTO import SeleniumDTO
from TargetDTO import TargetDTO
# Accept A SeleniumDTO object and an array of TargetDTO objects
class SeleniumAction:
def __init__(self, selenium_dto: SeleniumDTO, target_dto_list: List[TargetDTO]):
self.selenium_dto = selenium_dto
self.target_dto_list = target_dto_list
def run(self):
options = uc.ChromeOptions()
for argument in self.selenium_dto.arguments:
options.add_argument(argument)
driver = uc.Chrome(options=options)
for target_dto in self.target_dto_list:
if target_dto.wait_for_xpath_element is None:
driver.implicitly_wait(target_dto.timeout)
driver.get(target_dto.url)
if target_dto.wait_for_xpath_element is not None:
try:
WebDriverWait(driver, target_dto.timeout).until(
EC.visibility_of_element_located((By.XPATH, target_dto.wait_for_xpath_element))
)
except:
driver.save_screenshot(target_dto.screenshot_filename)
raise
if self.selenium_dto.fullscreen:
s = lambda x: driver.execute_script('return document.body.parentNode.scroll' + x)
driver.set_window_size(s('Width'), s('Height'))
driver.save_screenshot(target_dto.screenshot_filename)
else:
driver.save_screenshot(target_dto.screenshot_filename)
with open(target_dto.html_output_filename, "w") as f:
f.write(driver.page_source)
driver.quit()