Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fake_useragent import UserAgent
- from selenium import webdriver
- import time
- import random as rand
- import scipy.interpolate as si
- from selenium.common.exceptions import NoSuchElementException
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.select import Select
- import numpy as np
- from definitions import SRC_ROOT_DIR
- class BaseWebdriverRegistrator(object):
- WAIT_BETWEEN_INPUT = 2
- TIME_TO_WAIT = 20
- ATTEMPTS_TO_GET_PHONE = 5
- ADBLOCK_EXTENSION_PATH = SRC_ROOT_DIR + "/config/adblock.xpi"
- def __init__(self):
- self._setup_driver()
- def _wait(self, seconds=WAIT_BETWEEN_INPUT):
- time.sleep(seconds)
- def _setup_driver(self):
- profile = webdriver.FirefoxProfile()
- ua = UserAgent()
- profile.set_preference("general.useragent.override", ua.random)
- profile.set_preference("marionette.enabled", False)
- self.driver = webdriver.Firefox(profile)
- self.driver.install_addon(self.ADBLOCK_EXTENSION_PATH, True)
- self.driver.implicitly_wait(self.TIME_TO_WAIT)
- def restart_driver(self):
- self.driver.quit()
- self._setup_driver()
- def click_with_movement(self, element):
- self.move_mouse_to_element(element)
- element.click()
- def go_to_url(self, url):
- self.driver.get(url)
- def click_if_exists_by_class_name(self, class_name):
- try:
- element = self.driver.find_element_by_class_name(class_name)
- if element:
- element.click()
- except Exception:
- return False
- def click_if_exists_by_id(self, id):
- try:
- element = self.driver.find_element_by_id(id)
- if element:
- element.click()
- except Exception:
- return False
- def scroll_page_to_top(self):
- self.driver.execute_script("window.scrollTo(0, 0);")
- self._wait()
- def scroll_into_view(self,element):
- self.driver.execute_script("return arguments[0].scrollIntoView();", element)
- self._wait()
- def scroll_to_element_step_by_step(self, element, delta=10):
- x_el_postion = element.location['y']
- curr_offset = 0
- print("EL " + str(x_el_postion))
- while curr_offset <= 250:
- curr_offset += delta
- print("OFFSET " + str(curr_offset))
- self.driver.execute_script("window.scrollBy(0, " + str(curr_offset) + ")")
- time.sleep(1)
- self.move_mouse_to_element(element)
- def scroll_page_by_steps(self, steps_cnt=5, offset=250):
- for i in range(steps_cnt): # adjust integer value for need
- # you can change right side number for scroll convenience or destination
- self.driver.execute_script("window.scrollBy(0, " + str(offset) + ")")
- # you can change time integer to float or remove
- time.sleep(1)
- def get_element_by_class_selector(self, class_names):
- try:
- return self.driver.find_element_by_css_selector(class_names)
- except Exception:
- return False
- def refresh_page(self):
- self.driver.refresh()
- def element_exists_by_class_selector(self, class_names):
- try:
- return self.driver.find_element_by_css_selector(class_names)
- except Exception:
- return False
- def element_exists_by_class(self, class_name):
- try:
- return self.driver.find_element_by_class_name(class_name)
- except Exception:
- return False
- def click_if_exists_by_xpath(self, xpath):
- try:
- element = self.get_if_exists_by_xpath(xpath)
- if element:
- element.click()
- except Exception:
- return False
- def get_if_exists_by_xpath(self, xpath):
- try:
- return self.driver.find_element_by_xpath(xpath)
- except NoSuchElementException:
- return False
- def move_mouse_to_element(self, element):
- action = ActionChains(self.driver)
- action.move_to_element(element)
- action.perform()
- def _move_mouse_randomly(self, element):
- action = ActionChains(self.driver)
- action.move_to_element(element)
- action.perform()
- # Curve base:
- points = [[0, 0], [0, 2], [2, 3], [4, 0], [6, 3], [8, 2], [8, 0]];
- points = np.array(points)
- x = points[:, 0]
- y = points[:, 1]
- t = range(len(points))
- ipl_t = np.linspace(0.0, len(points) - 1, 100)
- x_tup = si.splrep(t, x, k=3)
- y_tup = si.splrep(t, y, k=3)
- x_list = list(x_tup)
- xl = x.tolist()
- x_list[1] = xl + [0.0, 0.0, 0.0, 0.0]
- y_list = list(y_tup)
- yl = y.tolist()
- y_list[1] = yl + [0.0, 0.0, 0.0, 0.0]
- x_i = si.splev(ipl_t, x_list) # x interpolate values
- y_i = si.splev(ipl_t, y_list) # y interpolate values
- for mouse_x, mouse_y in zip(x_i, y_i):
- action.move_by_offset(mouse_x, mouse_y)
- action.perform()
- print(mouse_x, mouse_y)
- def check_if_exists_by_id(self, id):
- try:
- self.driver.find_element_by_id(id)
- except NoSuchElementException:
- return False
- return True
- def check_exists_by_xpath(self, xpath):
- try:
- self.driver.find_element_by_xpath(xpath)
- except NoSuchElementException:
- return False
- return True
- def _send_keys_with_interval(self, text, element, delay=0.3):
- for character in text:
- element.send_keys(character)
- time.sleep(rand.uniform(0, delay))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement