How to bring selenium browser to the front?

2020-07-08 06:29发布

问题:

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.webdriver.common.by import By
# available since 2.4.0
from selenium.webdriver.support.ui import WebDriverWait
# available since 2.26.0
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()
browser.get(someWebPage)

element = WebDriverWait(browser, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Watch")]')))

elements = browser.find_elements_by_xpath('//button[contains(., "Watch")]')
if elements:
    print 'find watch button',elements
    elements[0].click()

it errors out with

  File "myScript.py", line 1469, in getSignedUrl
    EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Watch")]')))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    raise TimeoutException(message)
TimeoutException: Message: ''

but if i leave my browser window in front, it doesn't error out. is it because i should not use EC.element_to_be_clickable ? i tried ignoring the error and continue to click the button i want, but it says it can not find the button if the browser window is in background. so i think what i should do is before it comes to that line, i bring the browser window to foreground ?

i saw some people discussing switchTo() ? but my browser object doesn't have that method. what can i do to bring the window to front system independently? thanks

test system

python 2.7 windows 7 x64

python 2.6.6 CentOS 6.4

edit: i tried

browser.execute_script("window.focus();")

and

# commented out EC.element_to_be_clickable as suggested by alecxe
# element = WebDriverWait(browser, 20).until(
#     EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Watch")]')))
print 'sleeping 5'
time.sleep(5)
elements = browser.find_elements_by_xpath('//button[contains(., "Watch")]')

print 'before clicking'

from selenium.webdriver.common.action_chains import ActionChains
hover = ActionChains(browser).move_to_element(elements[0])
hover.perform()

print elements
elements[0].click()

not working

edit2: i checked the doc

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)[source]

    An Expectation for checking an element is visible and enabled such that you can click it.

it seems because when the window is in bg or minimized,the element is not "visible", so this condition is never met ? anyway i tried another condition

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)[source]

    An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located

it seems this one is working. previously i tried completely removing the condition and it doesn't work maybe simply because i didn't "wait" long (sleep 5 sec ) enough.

edit3: strangely the same code works fine on a computer with selenium 2.39 python 2.6.6 firefox 28 centos 6.4 even with browser window minimized. but same code tried another two machines failed, browser window has to be maximized and button has to be "visible" A. win7 x64 firefox 28 selenium 2.41 python2.7 B. Fedora 20 firefox 28 selenium 2.41 python2.7

回答1:

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()


回答2:

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



回答3:

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



回答4:

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!



回答5:

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.



回答6:

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/