Python Forum

Full Version: not abel to run selenium automation one by one in loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HELLO, i just learned python and this is my first project.
everything is working fine except looping part. if amount is 10 and i have 2 automation site functions. so as loop starts, loop count 1 and site function should be 1 and as as loop count 2 so site function 2 should start but its not working like that. as the loop starts site 1 function repeating 10 times then it goes to site 2 function, i dont want like this. i want to make it, 1 count = site 1 function, count 2 = site 2 function should work. PLEASE HELP. THANK YOU.

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

mobile_number = '9199999999'
amount = 10

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 
chrome_options.add_experimental_option("prefs",prefs)
browser = webdriver.Chrome(chrome_options=chrome_options)

def decathlon(mobile_number):         #SITE 1 FUNCTION
    for i in range(amount):        
        browser.get('https://www.decathlon.in/')
        time.sleep(2)        
        login = browser.find_element_by_class_name('m-0').click()
        time.sleep(1)
        number = browser.find_element_by_class_name('floating-input').send_keys(mobile_number)         
        button = browser.find_element_by_class_name('decabutton').click()      
        time.sleep(2)                   
        #browser.quit()

def shaze(mobile_number):         #SITE 2 FUNCTION
    for i in range(amount): 
        browser.get('https://www.shaze.in/customer/account/create/')
        time.sleep(2)
        number = browser.find_element_by_class_name('mobnumber').send_keys(mobile_number)
        time.sleep(1)         
        otp = browser.find_element_by_class_name('regi-sendotp').click()      
        time.sleep(2)         
        #browser.quit()

for i in range(amount):  # LOOP for repeating automation
    shaze(mobile_number)
    decathlon(mobile_number)
OK, I'm getting kind of confused while reading your post. So mayby I do not fully understand it well.

Are you saying that both of the site functions should NOT run 10 times, but just once? Because both of the functions are exactly implying that: a loop that is running 10 times.
(Apr-25-2020, 06:49 PM)Jeff900 Wrote: [ -> ]OK, I'm getting kind of confused while reading your post. So mayby I do not fully understand it well.

Are you saying that both of the site functions should NOT run 10 times, but just once? Because both of the functions are exactly implying that: a loop that is running 10 times.

if i said loop to run 10 times, then the loop should run 10 times but 1 by 1,

(1). if loop starts from 1 then site 1 function should run and when loop reach count 2 then site 2 function should start.

(2). but the loop not working like that, if i said to run 10 times then site 1 function runing 10 times then it going to site 2 function, it should work like (1).
OK, maybe you can use an if statement that will look if i (from the loop) is odd or even. You can use modulus for that.

if i % 2 == 0: 
    # execute function 2 
else:
    # excute function 1
If you can't figure this concept, find some info about modulus.
(Apr-25-2020, 07:07 PM)Jeff900 Wrote: [ -> ]OK, maybe you can use an if statement that will look if i (from the loop) is odd or even. You can use modulus for that.

if i % 2 == 0: 
    # execute function 2 
else:
    # excute function 1
If you can't figure this concept, find some info about modulus.

its still not working, same thing happening, every function repeating 2 times, i dont want like that. i want it to work like, 1 function should work once then it should go to other function.
I that case you should share the output you get, and explain where it is going wrong. Explain what output you expect. That will probably makes it a lot easier to understand the problem.