How to bring selenium browser to the front?

后端 未结 6 676
旧时难觅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:03

    To answer your original question, you need to use the maximize window method for the browser to regain focus and be placed in foreground. it should be something like this in Python:

    browser.maximize_window()
    
    0 讨论(0)
  • 2021-02-08 07:13

    Out of box, selenium does not expose driver process id or Browser hwnd but it is possible. Below is the logic to get hwnd

    • When driver is initialized, get the url for hub and extract the port number
    • From port number, find the process id which is using this port for listening, ie. PID of driver
    • After navigation, from all instances of iexplore find the parent PID matches the pid of driver, i.e browser pid.
    • Get the Hwnd of browser pid once browser hwnd is found , you can use win32 api to bring selenium to foreground.

    its not possible to post full code here, the full working solution (C#) to bring browser in front is on my blog

    http://www.pixytech.com/rajnish/2016/09/selenium-webdriver-get-browser-hwnd/

    0 讨论(0)
  • 2021-02-08 07:14

    Now I don't do python, but how I got around this in Groovy was the following:

    browser.js."alert()"
    webdriver.switchTo().alert().accept()
    

    I called the javascript alert function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept(). Hopefully there is something similar in Python.

    Hope this helps!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-08 07:25

    This is a complete hack, but it worked for me using IE, selenium remote driver in Windows 7.

    driver.get("about:blank")  
    driver.minimize_window()  
    driver.maximize_window()  
    driver.minimize_window()  
    driver.maximize_window()  
    

    I send a blank page, then minimize and maximize twice. It seems to result in the newly created IE having foreground focus.

    0 讨论(0)
  • 2021-02-08 07:25

    This worked for me to bring the browser window to the foreground. I tested using chromedriver on Selenium 3.6.0 on Python 3.

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    driver.switch_to.window(driver.current_window_handle)
    

    Related answer in Java - https://sqa.stackexchange.com/questions/16428/how-can-i-bring-chrome-browser-to-focus-when-running-a-selenium-test-using-chrom

    0 讨论(0)
提交回复
热议问题