Skip to content

Commit

Permalink
Added possibility to move the mouse negative values (#458)
Browse files Browse the repository at this point in the history
* Added possibility to move the mouse negative values

* - Refactored, moved mouse movement and path generation to own file mousemover.py

* - Reordered imports

* - Fix calls to move_mouse_to_coordinates

Co-authored-by: Juri <j.robl@osram.com>
  • Loading branch information
Genmutant and Juri committed Jun 15, 2021
1 parent 2db7f78 commit 312923a
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 152 deletions.
192 changes: 40 additions & 152 deletions tools/its.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Alphabetisch sortiert:
import copy
import json
import os
import platform
import string
Expand All @@ -11,7 +12,6 @@
from datetime import time as dtime
from itertools import cycle
from json import JSONDecodeError
import json
from random import choice, choices, randint

import cloudscraper
Expand All @@ -23,16 +23,15 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from seleniumwire import webdriver as selenium_wire

from tools.chromium_downloader import chromium_executable, check_chromium, webdriver_executable, check_webdriver
from tools.clog import CLogger
from tools.exceptions import AppointmentGone, BookingError, TimeframeMissed, UnmatchingCodeError
from tools.kontaktdaten import decode_wochentag, validate_codes, validate_kontakt, \
validate_zeitrahmen
from tools.mousemover import move_mouse_to_coordinates
from tools.utils import fire_notifications, unique
from tools.chromium_downloader import chromium_executable, check_chromium, webdriver_executable, check_webdriver


try:
import beepy
Expand Down Expand Up @@ -295,117 +294,6 @@ def get_chromedriver(self, headless):

return Chrome(self.get_chromedriver_path(), options=chrome_options)

def generate_way_between_coordinates(self, source_x: int, source_y: int, target_x: int, target_y: int) -> tuple:
"""Generate random waypoints between two x,y coordinates without numpy
Args:
source_x (int): x coordnate of source
source_y (int): y coordinate of source
target_x (int): x coordinate of target
target_y (int): y coordinate of target
Returns:
tuple: List of waypoints (x_coordinates, y_coordinates)
"""

# Init and add source coordinates
x_coordinates = [source_x]
y_coordinates = [source_y]

x_target_reached = False
y_target_reached = False

while not x_target_reached or not y_target_reached:

# Create new random waypoints
source_x = source_x + randint(3,50)
source_y = source_y + randint(3,50)

# If targets reached, stay at target
if source_x >= target_x:
source_x = target_x
x_target_reached = True
if source_y >= target_y:
source_y = target_y
y_target_reached = True

# Append new waypoint coordinates
x_coordinates.append(source_x)
y_coordinates.append(source_y)

return x_coordinates, y_coordinates

def move_mouse_by_offsets(self, x_coordinates: list, y_coordinates: list, driver) -> tuple:
"""Move mouse by offeset to list of x and y coordinates
Args:
x_coordinates (list): x waypoints
y_coordinates (list): y waypoints
driver : Chromedriver
Returns:
tuple: Current mouse coordinates (mouse_x, mouse_y)
"""

# Init current mouse position
current_mouse_x = 0
current_mouse_y = 0

# Append mouse movement for each x,y coordinate
for index, coordinate in enumerate(zip(x_coordinates, y_coordinates)):

# Get current window size
window_width = driver.get_window_size()["width"]
window_height = driver.get_window_size()["height"]

# If not first index calculate difference to last coordinate and update next offset
if index:
x_offset = coordinate[0] - x_coordinates[index - 1]
y_offset = coordinate[1] - y_coordinates[index - 1]
# First offset has no previous coordinate
else:
x_offset = coordinate[0]
y_offset = coordinate[1]

# Update predicted mouse position
current_mouse_x = current_mouse_x + x_offset
current_mouse_y = current_mouse_y + y_offset

# Check wether predicted mouse position is out of bounds
if not current_mouse_x >= window_width and not current_mouse_y >= window_height:
# Append mouse movements
try:
ActionChains(driver).move_by_offset(x_offset,y_offset).perform()

except MoveTargetOutOfBoundsException as e:
pass

time.sleep(randint(1,5))

return current_mouse_x, current_mouse_y

def move_mouse_to_coordinates(self, start_x: int, start_y: int, target_x: int, target_y: int, driver) -> tuple:
"""Move mouse from x,y coordinates to x,y coordinates
Args:
start_x (int): x coordinate of start position
start_y (int): y coordinate of start position
target_x (int): x coordinate of target position
target_y (int): y x coordinate of target position
driver : Chromedriver
Returns:
tuple: Current mouse coordinates (mouse_x, mouse_y)
"""

# Generate waypoints
coordinates_to_element = self.generate_way_between_coordinates(start_x, start_y, target_x, target_y)

self.log.info(f"Simulation der Mausbewegungen gestartet. Von: ({start_x}, {start_y}) nach ({target_x}, {target_y})")

# Execute movements and return coordinates
return self.move_mouse_by_offsets(coordinates_to_element[0], coordinates_to_element[1], driver)

def driver_enter_code(self, driver, impfzentrum, code):
"""
TODO xpath code auslagern
Expand Down Expand Up @@ -436,11 +324,11 @@ def driver_enter_code(self, driver, impfzentrum, code):


# random start position
current_mouse_positon = (randint(1,driver.get_window_size()["width"]-1),
randint(1,driver.get_window_size()["height"]-1))
current_mouse_positon = (randint(1, driver.get_window_size()["width"]-1),
randint(1, driver.get_window_size()["height"]-1))
# Simulation der Mausbewegung
current_mouse_positon = self.move_mouse_to_coordinates(0, 0, current_mouse_positon[0],
current_mouse_positon[1], driver)
current_mouse_positon = move_mouse_to_coordinates(self.log, 0, 0, current_mouse_positon[0],
current_mouse_positon[1], driver)

# Klick auf "Auswahl bestätigen" im Cookies-Banner
button_xpath = "//a[contains(@class,'cookies-info-close')][1]"
Expand All @@ -450,10 +338,10 @@ def driver_enter_code(self, driver, impfzentrum, code):

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(button_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.click(button).perform()

Expand All @@ -466,10 +354,10 @@ def driver_enter_code(self, driver, impfzentrum, code):

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(button_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.click(button).perform()

Expand All @@ -481,10 +369,10 @@ def driver_enter_code(self, driver, impfzentrum, code):

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(input_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.click(input_field).perform()

Expand Down Expand Up @@ -520,8 +408,8 @@ def driver_enter_code(self, driver, impfzentrum, code):

element = driver.find_element_by_xpath(button_xpath)
# Simulation der Mausbewegung
_ = self.move_mouse_to_coordinates(current_mouse_positon[0], current_mouse_positon[1],
element.location['x'], element.location['y'], driver)
_ = move_mouse_to_coordinates(self.log, current_mouse_positon[0], current_mouse_positon[1],
element.location['x'], element.location['y'], driver)
action.click(button).perform()

# Zweiter Klick-Versuch, falls Meldung "Es ist ein unerwarteter Fehler aufgetreten" erscheint
Expand Down Expand Up @@ -1161,16 +1049,16 @@ def selenium_code_anfordern(self, mail, telefonnummer,
driver.execute_script('window.sessionStorage.setItem("ets-session-its-cv-quick-check",\''+ ets_session_its_cv_quick_check +'\');')
self.log.info("\"ets-session-its-cv-quick-check\" Key:Value zum sessionStorage hinzugefügt.")

# Durch ets-session-its-cv-quick-check im SessionStorage kann direkt der Check aufgerufen werden
# Durch ets-session-its-cv-quick-check im SessionStorage kann direkt der Check aufgerufen werden
driver.get(f"{url}impftermine/check")
self.log.info("Überprüfung der Impfberechtigung übersprungen / Vorhandene Termine simuliert und impftermine/check geladen.")

# random start position
current_mouse_positon = (randint(1,driver.get_window_size()["width"]-1),
current_mouse_positon = (randint(1,driver.get_window_size()["width"]-1),
randint(1,driver.get_window_size()["height"]-1))

# Simulation der Mausbewegung
current_mouse_positon = self.move_mouse_to_coordinates(0, 0, current_mouse_positon[0],
current_mouse_positon = move_mouse_to_coordinates(self.log, 0, 0, current_mouse_positon[0],
current_mouse_positon[1], driver)

# Klick auf "Auswahl bestätigen" im Cookies-Banner
Expand All @@ -1181,9 +1069,9 @@ def selenium_code_anfordern(self, mail, telefonnummer,

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(button_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.click(button).perform()
Expand All @@ -1197,15 +1085,15 @@ def selenium_code_anfordern(self, mail, telefonnummer,

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(input_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.move_to_element(input_field).click().perform()
action.move_to_element(input_field).click().perform()

# Chars einzeln eingeben mit kleiner Pause
for char in data['email']:
for char in data['email']:
input_field.send_keys(char)
time.sleep(randint(500,1000)/1000)

Expand All @@ -1220,15 +1108,15 @@ def selenium_code_anfordern(self, mail, telefonnummer,

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(input_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.move_to_element(input_field).click().perform()
action.move_to_element(input_field).click().perform()

# Chars einzeln eingeben mit kleiner Pause
for char in data['phone'][3:]:
for char in data['phone'][3:]:
input_field.send_keys(char)
time.sleep(randint(500,1000)/1000)

Expand All @@ -1242,9 +1130,9 @@ def selenium_code_anfordern(self, mail, telefonnummer,

# Simulation der Mausbewegung
element = driver.find_element_by_xpath(button_xpath)
current_mouse_positon = self.move_mouse_to_coordinates(current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
current_mouse_positon = move_mouse_to_coordinates(self.log, current_mouse_positon[0],
current_mouse_positon[1],
element.location['x'],
element.location['y'], driver)

action.click(button).perform()
Expand Down
Loading

0 comments on commit 312923a

Please sign in to comment.