How to bring selenium browser to the front?

后端 未结 6 667
旧时难觅i
旧时难觅i 2021-02-08 06:24

if the browser window is in bg, then this line doesn\'t work.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webd         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-08 07:20

    Tested and works in Python 36

    driver1 = webdriver.Chrome()
    driver1.get("http://www.python.org")
    pyName = driver1.title
    pyHandle = driver1.window_handles[0]
    driver2 = webdriver.Chrome()
    driver2.get("http://www.ruby-lang.org/en")
    rubyName = driver2.title
    rubyHandle = driver2.window_handles[0]
    #driver 2 at this time will be in the foreground
    driver1.switch_to.window(pyHandle)
    driver1.switch_to_window(driver1.current_window_handle)
    

    Edit: switch_to_window is deprecated, use switch_to.window

提交回复
热议问题