Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring selenium browser to the front?

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

like image 722
Shuman Avatar asked Apr 21 '14 15:04

Shuman


1 Answers

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

like image 166
Taylor D. Edmiston Avatar answered Sep 29 '22 11:09

Taylor D. Edmiston