Using two webdrives in Selenium

Asked

Viewed 62 times

0

I need to develop a script for automation of a website. I get a list of URLs I pass this list on a for that for each item in the list a new one is created webdriver and it asks to login to the site. I thought to create two script one that makes the access to the site and the other to manipulate the site.

My attempt to use two webdrivers and make the second maximize the window created by the first, will always be created a new webdriver function call. If you know a better suggestion please say.

def teste(browser,link,i):
    browser.execute_script("window.open('"+link+"','new window','width=200, height=100')")
    browser1 = webdriver.Firefox()
    browser1.switch_to_window(browser.window_handles[i])
    browser1.maximize_window()

Note: The list is too long, I’m using multithreads to speed up the process.

  • 1

    Only one login required? If yes, you can reuse cookies from one login in the other instances.

  • I was able to resolve with your idea of using cookies

1 answer

1


Following the idea of Pedro I used cookies. To do this I created two functions to s_cookies() and the l_cookies the first saves the cookies of the page already logged in and loaded and the second loads the cookies to page every time it is accessed.

import pickle
import selenium
from selenium import webdriver

def s_cookies(self,browser, loc):
    pickle.dump(browser.get_cookies(), open(loc,'wb'))

def l_cookies(self,browser, loc, url=None):
    cookies = pickle.load(open(loc,'rb'))

    url = "https://google.com" if url is None else url
    browser.get(url)
    for cookie in cookies:
        browser.add_cookie(cookie)

Note: It only worked with the webdriver Chrome and as I was with a tight deadline did not want me to delve deeper pq did not work on Firefox

Note: I recommend calling the function l_cookies() right after instantiating the webdriver`

Browser other questions tagged

You are not signed in. Login or sign up in order to post.