2017-04-26 77 views
1

我想在使用硒的多个浏览器上运行我的脚本。 截至目前,我能够通过一次打开一个浏览器来执行操作。 例如: - 注册到亚马逊。 我希望能够同时注册两个用户到亚马逊。使用硒在多个浏览器上同时运行python SCRIPT

这是我现在的代码。

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) 

通过使用这个我可以一次运行一个用户。但我希望能够同时注册两个以上的用户到多达n个用户。 因此,多窗口的问题。

+0

的可能的复制[如何同时运行多个硒Firefox浏览器?(http://stackoverflow.com/questions/16551111/how-to-run-multiple-selenium-firefox-browsers-同时) – Windmill

回答

2

您可以创建多个webdriver实例。然后你可以单独操纵每一个。例如,

from selenium import webdriver 
driver1 = webdriver.Chrome() 
driver2 = webdriver.Chrome() 
driver1.get("http://google.com") 
driver2.get("http://yahoo.com") 
+0

正是我想到的,但是这又一次执行数据,而不是并行。 我也想到循环的概念。 – Abul

+0

查看http://stackoverflow.com/questions/16551111/how-to-run-multiple-selenium-firefox-browsers-与此同时。另外,Selenium Grid 2似乎是这样做的常用方式。请参阅https://www.gridlastic.com/python-code-example.html – Windmill

+0

Selemium Grid适用于多线程,对于Abul所需要的,这不是一个好方法。实例化webdriver的多个实例不会解决问题,因为这将是一个线程,并且线将逐个执行。目前最好的解决方案是在python本身(v3.4及更高版本)中调用'Asynchronous Executing'。研究这两个好的资源,你就可以完成这个工作:[一个使用asyncio在Python中进行异步编程的指南](https://medium.freecodecamp.org/a-guide-to-asynchronous-programming-in-python -with-asyncio-232e2afa44f6) – Ibo

相关问题