python - How to bring selenium browser to the front? -
if browser window in bg, line doesn't work.
from selenium import webdriver selenium.webdriver.common.keys import keys selenium.webdriver.common.by import # available since 2.4.0 selenium.webdriver.support.ui import webdriverwait # available since 2.26.0 selenium.webdriver.support import expected_conditions 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
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 leave browser window in front, doesn't error out. because should not use ec.element_to_be_clickable
? tried ignoring error , continue click button want, says can not find button if browser window in background. think should before comes line, bring browser window foreground ?
i saw people discussing switchto()
? browser object doesn't have method. can bring window front system independently?
test system
python 2.7 windows 7 x64
python 2.6.6 centos 6.4
edit: tried
browser.execute_script("window.focus();")
and
# commented out ec.element_to_be_clickable suggested 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' 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: checked doc
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)[source] expectation checking element visible , enabled such can click it.
it seems because when window in bg or minimized,the element not "visible", condition never met ? anyway tried condition
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)[source] expectation checking element present on dom of page. not mean element visible. locator - used find element returns webelement once located
it seems 1 working. tried removing condition , doesn't work maybe because didn't "wait" long (sleep 5 sec ) enough.
edit3: strangely same code works fine on computer selenium 2.39 python 2.6.6 firefox 28 centos 6.4 browser window minimized. same code tried 2 machines failed, browser window has maximized , button has "visible" a. win7 x64 firefox 28 selenium 2.41 python2.7 b. fedora 20 firefox 28 selenium 2.41 python2.7
to answer original question, need use maximize window method browser regain focus , placed in foreground. should in python:
browser.maximize_window()
Comments
Post a Comment