Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. from fake_useragent import UserAgent
  2. from selenium import webdriver
  3. import time
  4. import random as rand
  5.  
  6. import scipy.interpolate as si
  7. from selenium.common.exceptions import NoSuchElementException
  8. from selenium.webdriver import ActionChains
  9. from selenium.webdriver.common.keys import Keys
  10. from selenium.webdriver.support.select import Select
  11. import numpy as np
  12.  
  13. from definitions import SRC_ROOT_DIR
  14.  
  15.  
  16. class BaseWebdriverRegistrator(object):
  17. WAIT_BETWEEN_INPUT = 2
  18. TIME_TO_WAIT = 20
  19. ATTEMPTS_TO_GET_PHONE = 5
  20. ADBLOCK_EXTENSION_PATH = SRC_ROOT_DIR + "/config/adblock.xpi"
  21.  
  22. def __init__(self):
  23. self._setup_driver()
  24.  
  25. def _wait(self, seconds=WAIT_BETWEEN_INPUT):
  26. time.sleep(seconds)
  27.  
  28. def _setup_driver(self):
  29. profile = webdriver.FirefoxProfile()
  30. ua = UserAgent()
  31. profile.set_preference("general.useragent.override", ua.random)
  32. profile.set_preference("marionette.enabled", False)
  33. self.driver = webdriver.Firefox(profile)
  34. self.driver.install_addon(self.ADBLOCK_EXTENSION_PATH, True)
  35. self.driver.implicitly_wait(self.TIME_TO_WAIT)
  36.  
  37. def restart_driver(self):
  38. self.driver.quit()
  39. self._setup_driver()
  40.  
  41. def click_with_movement(self, element):
  42. self.move_mouse_to_element(element)
  43. element.click()
  44.  
  45. def go_to_url(self, url):
  46. self.driver.get(url)
  47.  
  48. def click_if_exists_by_class_name(self, class_name):
  49. try:
  50. element = self.driver.find_element_by_class_name(class_name)
  51. if element:
  52. element.click()
  53. except Exception:
  54. return False
  55.  
  56. def click_if_exists_by_id(self, id):
  57. try:
  58. element = self.driver.find_element_by_id(id)
  59. if element:
  60. element.click()
  61. except Exception:
  62. return False
  63.  
  64. def scroll_page_to_top(self):
  65. self.driver.execute_script("window.scrollTo(0, 0);")
  66. self._wait()
  67.  
  68. def scroll_into_view(self,element):
  69. self.driver.execute_script("return arguments[0].scrollIntoView();", element)
  70. self._wait()
  71.  
  72. def scroll_to_element_step_by_step(self, element, delta=10):
  73. x_el_postion = element.location['y']
  74. curr_offset = 0
  75. print("EL " + str(x_el_postion))
  76. while curr_offset <= 250:
  77. curr_offset += delta
  78. print("OFFSET " + str(curr_offset))
  79. self.driver.execute_script("window.scrollBy(0, " + str(curr_offset) + ")")
  80. time.sleep(1)
  81. self.move_mouse_to_element(element)
  82.  
  83. def scroll_page_by_steps(self, steps_cnt=5, offset=250):
  84. for i in range(steps_cnt): # adjust integer value for need
  85. # you can change right side number for scroll convenience or destination
  86. self.driver.execute_script("window.scrollBy(0, " + str(offset) + ")")
  87. # you can change time integer to float or remove
  88. time.sleep(1)
  89.  
  90. def get_element_by_class_selector(self, class_names):
  91. try:
  92. return self.driver.find_element_by_css_selector(class_names)
  93. except Exception:
  94. return False
  95.  
  96. def refresh_page(self):
  97. self.driver.refresh()
  98.  
  99. def element_exists_by_class_selector(self, class_names):
  100. try:
  101. return self.driver.find_element_by_css_selector(class_names)
  102. except Exception:
  103. return False
  104.  
  105. def element_exists_by_class(self, class_name):
  106. try:
  107. return self.driver.find_element_by_class_name(class_name)
  108. except Exception:
  109. return False
  110.  
  111. def click_if_exists_by_xpath(self, xpath):
  112. try:
  113. element = self.get_if_exists_by_xpath(xpath)
  114. if element:
  115. element.click()
  116. except Exception:
  117. return False
  118.  
  119. def get_if_exists_by_xpath(self, xpath):
  120. try:
  121. return self.driver.find_element_by_xpath(xpath)
  122. except NoSuchElementException:
  123. return False
  124.  
  125. def move_mouse_to_element(self, element):
  126. action = ActionChains(self.driver)
  127. action.move_to_element(element)
  128. action.perform()
  129.  
  130. def _move_mouse_randomly(self, element):
  131. action = ActionChains(self.driver)
  132. action.move_to_element(element)
  133. action.perform()
  134. # Curve base:
  135. points = [[0, 0], [0, 2], [2, 3], [4, 0], [6, 3], [8, 2], [8, 0]];
  136. points = np.array(points)
  137.  
  138. x = points[:, 0]
  139. y = points[:, 1]
  140.  
  141. t = range(len(points))
  142. ipl_t = np.linspace(0.0, len(points) - 1, 100)
  143.  
  144. x_tup = si.splrep(t, x, k=3)
  145. y_tup = si.splrep(t, y, k=3)
  146.  
  147. x_list = list(x_tup)
  148. xl = x.tolist()
  149. x_list[1] = xl + [0.0, 0.0, 0.0, 0.0]
  150.  
  151. y_list = list(y_tup)
  152. yl = y.tolist()
  153. y_list[1] = yl + [0.0, 0.0, 0.0, 0.0]
  154.  
  155. x_i = si.splev(ipl_t, x_list) # x interpolate values
  156. y_i = si.splev(ipl_t, y_list) # y interpolate values
  157.  
  158. for mouse_x, mouse_y in zip(x_i, y_i):
  159. action.move_by_offset(mouse_x, mouse_y)
  160. action.perform()
  161. print(mouse_x, mouse_y)
  162.  
  163. def check_if_exists_by_id(self, id):
  164. try:
  165. self.driver.find_element_by_id(id)
  166. except NoSuchElementException:
  167. return False
  168. return True
  169.  
  170. def check_exists_by_xpath(self, xpath):
  171. try:
  172. self.driver.find_element_by_xpath(xpath)
  173. except NoSuchElementException:
  174. return False
  175. return True
  176.  
  177. def _send_keys_with_interval(self, text, element, delay=0.3):
  178. for character in text:
  179. element.send_keys(character)
  180. time.sleep(rand.uniform(0, delay))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement