2

I would like to run my script on Multiple browser using selenium. As of now I am able to perform the operation by opening one browser at a time. Eg:- Register to amazon. I want to be able to Register two users to amazon at the same time.

This is the code I have as of now.

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
driver.get("https://www.amazon.com/ap/register?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&prevRID=VBHFJ50CPKFJ3PGG7RDY&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&prepopulatedLoginId=&failedSignInCount=0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=usflex&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0")
            driver.find_element_by_xpath("""//*[@id="s2id_ID_form4a8055de_guest_register_sponsor_lookup"]/a/span[2]/b""").click()
            driver.find_element_by_xpath("""//*[@id="s2id_autogen1_search"]""").send_keys(v1)

By using this I can run it for one user at one time. But I want to be able to register more than two users upto n users at the same time. Hence, the multiple windows questions.

Abul
  • 197
  • 2
  • 4
  • 15
  • Possible duplicate of [How to run multiple Selenium Firefox browsers concurrently?](http://stackoverflow.com/questions/16551111/how-to-run-multiple-selenium-firefox-browsers-concurrently) – Deem Apr 26 '17 at 06:42

2 Answers2

3

You could create multiple instances of the webdriver. You can then manipulate each individually. For example,

from selenium import webdriver
driver1 = webdriver.Chrome()
driver2 = webdriver.Chrome()
driver1.get("http://google.com")
driver2.get("http://yahoo.com")
Deem
  • 7,007
  • 2
  • 19
  • 23
  • 1
    Exactly what I thought of, but this again executes the data sequentially and not parallel. I also thought of loops concept. – Abul Apr 26 '17 at 06:37
  • Check out http://stackoverflow.com/questions/16551111/how-to-run-multiple-selenium-firefox-browsers-concurrently. Also, Selenium Grid 2 seems like a common way this is done. See https://www.gridlastic.com/python-code-example.html – Deem Apr 26 '17 at 06:41
  • Selemium Grid is for multi threading, which is not really a good approach for what Abul wants. Instantiating multiple instances of webdriver will not solve the problem since this will be one thread and lines will be executed one by one. The best solution is right now in python itself (v 3.4 and higher) that is called `Asynchronous Executing`. Study these two good sources and you will be able to get this done: [A guide to asynchronous programming in Python with asyncio](https://medium.freecodecamp.org/a-guide-to-asynchronous-programming-in-python-with-asyncio-232e2afa44f6) – Ibo Oct 19 '17 at 06:16
1

This question is a bit old at this point, but I still found it applicable to something I was having trouble with today.

In order to achieve parallel processes you need to utilize multiprocessing. Essentially, this allows you to create browser instances for each function and allow each script to lock to each browser GIL separately. You can then start each of the processes in your main code and they will all execute in parallel.

If you need an explanation on how to do this, a great video can be found here

johne518
  • 77
  • 5