iselenium.py 3.6 KB
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC

class SeleniumInterface:

	Chrome = "Chrome"
	By = By()
	Keys = Keys()
	Timeout = 10

	def __init__(self, driverType):
		self.driver = self._getDriver(driverType)

	def _getDriver(self, d):
		if d == SeleniumInterface.Chrome:
			return webdriver.Chrome()
		raise NoSuchDriver(self.driver)

	def get(self, url):
		self.driver.get(url)

	def find(self, by, lookup, select = "1"):
		return self.findFromElement(self.driver, by, lookup, select)

	def parent(self, element):
		return self.findFromElement(element, SeleniumInterface.By.XPATH, "parent::*")

	def children(self, element):
		return self.findFromElement(element, SeleniumInterface.By.XPATH, "child::*", "1-")

	def findFromElement(self, element, by, lookup, select = "1"):
		self._wait(element, (by, lookup))
		elements = element.find_elements(by, lookup)
		
		array = []
		for selector in select.replace(" ","").split(","):
			array += self._resolve(selector, elements)

		# One element expected
		if "-" not in select and "," not in select:
			return array[0]
		return array


	def write(self, element, keys, clear = True):
		if clear:
			element.clear()

		if type(keys) == list:
			for k in keys:
				self.write(element, k, clear = False)
		
		element.send_keys(keys)

	def _resolve(self, selector, elements):
		div, rev, answer = selector.split("-"), False, []
		try:
			# Not a range
			if len(div) == 1:
				return [ elements[int(div[0]) - 1] ]

			# Range	
			b = (1 if div[0] == "" else int(div[0]))
			e = (len(elements) if div[1] == "" else int(div[1]))

			if b > e:
				b, e = e, b
				rev = True

			for i in range(b - 1, e):
				if i < 0 or i >= len(elements):
					continue
				answer.append(elements[i])
			
			if rev:
				answer.reverse()

			return answer
		except:
			raise BadInterval(selector)
	
	def _wait(self, fromElement, locator):
		Wait(fromElement, SeleniumInterface.Timeout).until(
			EC.presence_of_all_elements_located(locator)
		)

	def __del__(self):
		self.driver.close()


class NoSuchDriver(Exception):

	def __init__(self, driver):
		self.driver = driver

	def __str__(self):
		return f"The driver '{self.driver}' is not supported"

class BadInterval(Exception):

	def __init__(self, interval):
		self.interval = interval

	def __str__(self):
		return f"The interval '{self.interval}' is not valid"

class NotEditable(Exception):

	def __init__(self, plate):
		self.plate = plate

	def __str__(self):
		return f"The plate '{self.plate}' is not editable"


if __name__ == "__main__":
	plate = "OQY729"
	s = SeleniumInterface(SeleniumInterface.Chrome)

	### Login ###
	s.get("https://rto.cent.gov.ar/rto")

	login = s.find(SeleniumInterface.By.NAME, "j_username")
	s.write(login, "salvatellih")

	psw = s.find(SeleniumInterface.By.NAME, "j_password")
	s.write(psw, "3")

	button = s.find(SeleniumInterface.By.ID, "submit")
	button.click()
	### Login ###

	### Get to Data ###
	s.get("https://rto.cent.gov.ar/rto/RTO/listaDePlanillas")

	# children of parent of td with innerText = plate
	columns = s.children(s.parent( s.find(SeleniumInterface.By.XPATH, f"//tr//td[text()='{plate}']") ))

	# get all a tags and click the last one
	options = s.findFromElement(columns[-1], SeleniumInterface.By.TAG_NAME, "a", "1-")
	options[-1].click()

	# get the 'Datos Técnicos' tag and click it
	tec = s.find(SeleniumInterface.By.XPATH, "//a[text()='ir a Datos Técnicos']")
	tec.click()
	### Get to Data ###

	### Read Data ###
	
	### Read Data ###