Selenium,将无头镀铬驱动器转变为用户可以与之交互的常规驱动器?

时间:2017-11-19 07:46:58

标签: python google-chrome selenium driver

我的目标是在一个定期刷新的页面上运行大约二十几个无头镀铬驱动程序。一旦驱动程序检测到页面源中出现的每个会话随机发生的某个关键字,我希望无头驱动程序能够被用户交互。

将无头镀铬驱动器变成常规镀铬驱动程序,用户可以看到并与可行的交互?我可能有用的另一个想法是将无头镀铬驱动程序的会话和cookie“转移”到常规的chrome驱动程序中,但同样,我不确定这是多么可行。

我想在第一部分使用无头驱动程序,因为它们不会使屏幕变得杂乱而且速度很快。

1 个答案:

答案 0 :(得分:0)

好的,我想出了一个很酷的工作。希望这有助于任何有同样问题的人! :)

user_agent = # user agent 
chrome_path= # path to chrome driver

# headless driver
chrome_options=Options()
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("headless")
chrome_options.add_argument('user-agent={'+user_agent+'}')
driver1=webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)

# let headless driver do its thing..., like log in.. etc

# switch to interactive browser when ready
profile_path=str(driver1.capabilities["chrome"]["userDataDir"])
chrome_options=Options()
chrome_options.add_argument("user-data-dir="+profile_path)
chrome_options.add_argument('user-agent={'+user_agent+'}')
chrome_options.add_argument("disable-infobars")
driver=webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
driver.get(url)
for cookie in [i for i in driver1.get_cookies() if i not in driver.get_cookies()]:
    driver.add_cookie(cookie)
driver.refresh() # should be what you expect :)