Python Selenium. Parallelize iteration from excel

Viewed 17

Please help.

I am trying to iterate through an excel file made up of 3 columns (IP, Username, Password). The script should read the excel fill line by line.

For line 1 from example, it will open a browser and go to the website with IP address in rows one and when the browser is fully loaded, enter the username and password then submit. Then go to the next line, open a browser, enter the IP and the enter the username and password and submit and then to the next line and so on...

screenshot of the excel I'm working with:

I am about to write a code the does the above sequentially (one after another). But i have challenges writing a code that can do that in parallel for example open 4 browsers at once and run the first 4 lines of the excel and when it is done, open the next 4 browsers for the next 4 lines.

Here is my code below. Please help!!

import PySimpleGUI as sg
import pandas as pd
import xlsxwriter
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver import DesiredCapabilities
import time
import os.path

ExcelPath = os.path.join( os.getcwd(), 'Data_Config.xlsx' )
df = pd.read_excel(ExcelPath, sheet_name='Sheet', engine="openpyxl", dtype=str) # read excel and convert all data to string
wb = openpyxl.load_workbook(ExcelPath)
ws = wb.worksheets[0]
maxrow = ws.max_row

layout = [
         [sg.Text('Please Select The Web Browser To Use')],
         [sg.Button('Chrome')],
         [sg.Button('Edge')],
         [sg.Button('Firefox')],
         [sg.Button('Internet Explorer')],
         ]
 window = sg.Window('Select Browser', layout, size=(520,150), element_justification='center')

                        while True:
                                event2, values = window.read()
                                if event2 in (sg.WINDOW_CLOSED, "Quit"):
                                    break

                                if event2 == 'Chrome':
                                    capabilities = DesiredCapabilities.CHROME.copy()
                                    capabilities["acceptInsecureCerts"] = True  
                                    driver = webdriver.Chrome(desired_capabilities=capabilities)

                                if event2 == 'Edge':
                                    capabilities = webdriver.DesiredCapabilities().EDGE
                                    capabilities['acceptSslCerts'] = True  
                                    driver = webdriver.Edge(capabilities=capabilities)
                                    driver = webdriver.Edge()

                                if event2 == 'Firefox':
                                    capabilities = webdriver.DesiredCapabilities().FIREFOX
                                    capabilities['acceptSslCerts'] = True  
                                    driver = webdriver.Firefox(capabilities=capabilities)
                                    driver = webdriver.Firefox()


                                # looping through all the data

                                for i in range(2, maxrow + 1):
                                 # Every Time Browser will open
                                    driver.switch_to.new_window('tab')
                                    entry = df.loc[i-2]
                                    try:
                                        driver.get('https://'+entry[IP])
                                       
                                        wait = WebDriverWait(driver,30)
                                        wait.until(EC.element_to_be_clickable((By.ID, 'username')))
                                        Username_input = driver.find_element_by_id('username')
                                        Username_input.send_keys(entry[Username])

                                        time.sleep(1)
                                        Password_input = driver.find_element_by_id('password')
                                        Password_input.send_keys(entry[Password])

                                        time.sleep(1)
                                        SignIn_btn = driver.find_element_by_id('bt-login')
                                        SignIn_btn.click()

                                    except ValueError as error:
                                        print("error")
0 Answers
Related