Class Def Python Try / Except

0

I'm looking for someone to help me with funciones and clases python. I had always worked with functions but never with functions within functions. When I wanted to run the program I did not perform any of my functions until I changed the def for class as you can see in the code. I'm looking to change this so that I can send the parameters in the def main directly that I want my variables to take within the functions. To be more clear I leave the code as I would like it to be. I need it to be this way since I work with databases that come from another file and I already have everything assembled my database from which I get the parameters from def mail()

Greetings

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')
timeout = 30
driver.get("https://onevideo.aol.com/#/")


def main():
    login()
    clone()
    Name()
    Rate()
    Floor()
    Download()


class login():
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "input.ng-valid:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)
    except TimeoutException:
        print ("Timed out waiting for page to load")

    username = driver.find_element_by_css_selector("input.ng-valid:nth-child(1)")
    username.send_keys("martinbouhier")
    driver.find_element_by_css_selector("button.btn:nth-child(2)").click()
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "div.group-field-block:nth-child(2) > input:nth-child(2)"))
        WebDriverWait(driver, timeout).until(element_present)
    except TimeoutException:
        print ("Timed out waiting for page to load")

    password = driver.find_element_by_css_selector("div.group-field-block:nth-child(2) > input:nth-child(2)")
    password.send_keys("pass")
    driver.find_element_by_css_selector(".button").click()
    element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'h1.ng-scope > span:nth-child(1)')))


class clone():
#Clonar
    url = driver.get("https://onevideo.aol.com/#/inventorysources")
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "div.ngRow:nth-child(1) > div:nth-child(1) > div:nth-child(9) > div:nth-child(1) > div:nth-child(1) > adap-options-menu:nth-child(1) > div:nth-child(1) > button:nth-child(2)"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")
    driver.find_element_by_css_selector('div.ngRow:nth-child(1) > div:nth-child(1) > div:nth-child(9) > div:nth-child(1) > div:nth-child(1) > adap-options-menu:nth-child(1) > div:nth-child(1) > button:nth-child(2)').click()
    try:
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "div.ngRow:nth-child(1) > div:nth-child(1) > div:nth-child(9) > div:nth-child(1) > div:nth-child(1) > adap-options-menu:nth-child(1) > div:nth-child(1) > button:nth-child(2)"))
        WebDriverWait(driver, timeout).until(element_present)

    except TimeoutException:
        print ("Timed out waiting for page to load")    
    driver.find_element_by_css_selector('.open > ul:nth-child(5) > li:nth-child(3) > a:nth-child(1)').click()

I need it to be something like this, where I work all the data in my data base in the run ():

def run(d):
    NewTag("url")
    Device = str(d.get("Li_platform")) #Mobile Web 
    Inicio_Li_Platform = ('//*[@id="platform_listbox"]/li[')
    Final_Li_Platform = (']')
    Platform(Inicio_Li_Platform+""+Device+""+Final_Li_Platform)
    NameTag(d.get("TagName"))
    Floor_Rate(d.get("Floor_price"))
    Save_Tag('save')
    Inicio_Li_Publisher = ('//*[@id="publisher_listbox"]/li[')
    Final_Li_Publisher = (']')
    Publisher = str(d.get("Li_Publisher")) #Publisher Altitude
    Publisher_Li(Inicio_Li_Publisher+""+Publisher+""+Final_Li_Publisher)
    Tag_Rate_and_Save(d.get("Tag_Rate"))
    time.sleep(2)


def main():
    login("martin", "pass")
    tags = (nametags)

    for tag in (tag.strip() for tag in tags.split(",")):
        d = (Inventory_data.get("Inventario_datos")).get(tag, None)
        if d:
            run(d)
        else:
            print('Error: Clave "{}" no encontrada.'.format(tag))
    driver.close()


def login(email, passwd):
        url = "url/login/"
        email_find = '//*[@id="form_username"]'
        passwd_find = '//*[@id="form_password"]'
        boton_find = '//*[@id="login_submit"]'
        driver.get(url)
        user = driver.find_element_by_xpath(email_find)
        user.send_keys(email)
        password = driver.find_element_by_xpath(passwd_find)
        password.send_keys(passwd)
        boton_find = driver.find_element_by_xpath(boton_find).click()
    
asked by Martin Bouhier 13.10.2017 в 18:08
source

1 answer

0

Martin is confusing class with def . The word class is used to define classes for object-oriented programming ( POO ) you do not have to put code as if it were a function. A class to give you an idea is an entity that serves to define an object such as person:

class Persona:
    """Simple clase de ejemplo"""
    edad = 34
    def saludar_a_alguien(self, nombre):
        return "hola {}".format(nombre)

    def saludar(self):
        return 'hola mundo'

eduardo = Persona()
print(eduardo.saludar())

I recommend that you read the official python documentation on classes But it is a programming paradigm that goes beyond python.

    
answered by 16.10.2017 в 07:02