Wait for answers to continue Selenium Python

1

I want to make this code with functions. With the data of the Login and achieve it and it is perfect, the problem comes later with 2 issues: The first, where it says #Clonar there is a change of URL and it is executed automatically once I click to login. That does not finish logging so I need a function that is not time.sleep() but one that waits until the login ends or when "onevideo.aol.com/#/Campaigns" is the page where I am in selenium (after login enter that url) there my driver.get("https://onevideo.aol.com/#/inventorysources") is executed.

On the other hand, here's my second question. I need just as I did for the login to do the same with #Clonar, #Name tag, #Rate, #Floor. What do I mean by 'The same', is that to execute each step until you get the answer do not run the following

Thanks

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')
driver.get("https://onevideo.aol.com/#/")
timeout = 30

def 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()

#Clonar
driver.get("https://onevideo.aol.com/#/inventorysources")
clone = 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()
clone = driver.find_element_by_css_selector('.open > ul:nth-child(5) > li:nth-child(3) > a:nth-child(1)').click()

#Nombre tag
name = driver.find_element_by_css_selector('div.margin-top-medium > input:nth-child(1)')
name.clear()
name.send_keys('--test$2--')

#Rate
Rate = driver.find_element_by_css_selector('.span9 > div:nth-child(1) > form:nth-child(1) > div:nth-child(9) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > span:nth-child(1) > adap-input:nth-child(1) > span:nth-child(1) > input:nth-child(2)')
Rate.clear()
Rate.send_keys('2')

#Floor
Floor = driver.find_element_by_css_selector('.span9 > div:nth-child(1) > form:nth-child(1) > div:nth-child(12) > div:nth-child(2) > adap-input:nth-child(1) > span:nth-child(1) > input:nth-child(1)')
Floor.clear()
Floor.send_keys('2')

driver.find_element_by_css_selector(".bs-docs-social-buttons > li:nth-child(2) > button:nth-child(1)").click()

driver.close()
    
asked by Martin Bouhier 12.10.2017 в 17:42
source

1 answer

0

You can apply exactly the same idea to that in the login, use a condition and an explicit wait.

You can use presence_of_element_located with any element of the page, so that you wait until it is available. There are more conditions, you can see in the documentation of selenium.webdriver.support.expected_conditions .

The wait explicit, unlike an implicit one or what time.sleep does (although this is blocking), wait for the time that you define before throwing an exception but at the moment in which the condition is fulfilled with execution immediately (instead of waiting for the complete timeout ).

What you can do is choose some element of the page onevideo.aol.com/#/Campaigns (an input, a button, text at the end of the page, etc), and make an explicit wait using it. This is done at the end of your function login (after the button is pressed), so it will not return until this element is present and, therefore, the page is loaded.

def 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_present = EC.presence_of_element_located((tiposelector, selector))
    WebDriverWait(driver, timeout).until(element_present)

Changing tiposelector and selector to the appropriate variables, looking for an element of the page that loads after the login.

You can specify the waiting time you want, if it is negative the waiting time will be infinite.

After the page is loaded login will return (or an exception is thrown if the time is exceeded) and you can continue with the rest ( clone ). You can put the rest of the code into functions and call them one after the other. Apparently you limit yourself to entering text in inputs so in theory you do not need more waits, however the idea is the same wherever you need it.

    
answered by 12.10.2017 / 20:14
source