如何将Selenium浏览器置于最前?

23 投票
9 回答
31930 浏览
提问于 2025-04-18 03:37

如果浏览器窗口在后台,那么这一行代码就不管用了。

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

它会报错,错误信息是

  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: ''

但是如果我把浏览器窗口放在前面,它就不会报错。这是因为我不应该使用 EC.element_to_be_clickable 吗?我试着忽略这个错误,继续点击我想要的按钮,但它说在浏览器窗口在后台时找不到这个按钮。所以我觉得我应该在到达那一行代码之前,把浏览器窗口调到前面?

我看到有人讨论过 switchTo(),但是我的浏览器对象没有这个方法。我该怎么做才能让窗口在系统中独立地显示在前面呢?谢谢。

测试系统

python 2.7 windows 7 x64

python 2.6.6 CentOS 6.4

编辑:我试过

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

还有

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

都不行。

编辑2:我查了文档

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.

似乎是因为当窗口在后台或被最小化时,元素并不可见,所以这个条件永远不会满足?无论如何,我试了另一个条件

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

这个条件似乎有效。之前我试着完全去掉这个条件,结果不行,可能是因为我没有“等待”足够长的时间(睡眠5秒)。

编辑3:奇怪的是,同样的代码在一台使用selenium 2.39、python 2.6.6、firefox 28、centos 6.4的电脑上即使浏览器窗口最小化也能正常工作。但是在另外两台机器上同样的代码就不行,浏览器窗口必须最大化,按钮必须“可见”。

A. win7 x64 firefox 28 selenium 2.41 python2.7

B. Fedora 20 firefox 28 selenium 2.41 python2.7

9 个回答

3

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

补充说明:switch_to_window这个方法已经不再推荐使用,应该改用switch_to.window。

3

我不太懂Python,但我在Groovy中是这样解决这个问题的:

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

我调用了JavaScript的alert函数,这样就把窗口调到了最前面,然后我用webdriver.switchTo().alert().accept()来关闭这个提示框。希望在Python中也有类似的办法。

希望这对你有帮助!

4

在C#中,使用

Driver.SwitchTo().Window ( Driver.CurrentWindowHandle );

并没有把窗口放到最前面,而只是让它在其他窗口中获得了焦点,仍然保持在它的Z轴顺序上。

有效的代码是:

protected void BringWindowToFront()
{
    var window = Driver.Manage ().Window;
    var position = window.Position;
    window.Minimize();
    window.Position = position;
}
7

要回答你最初的问题,你需要使用浏览器的最大化窗口方法,这样才能让它重新获得焦点并显示在最前面。用Python来实现的话,大概是这样的:

browser.maximize_window()
14

这个方法对我有效,可以把浏览器窗口调到最前面。我是在Python 3上使用Selenium 3.6.0和chromedriver测试的。

from selenium import webdriver

driver = webdriver.Chrome()

driver.switch_to.window(driver.current_window_handle)

相关的Java答案 - https://sqa.stackexchange.com/questions/16428/how-can-i-bring-chrome-browser-to-focus-when-running-a-selenium-test-using-chrom

撰写回答