如何将selenium浏览器置于最前面?

问题描述 投票:0回答:9

如果浏览器窗口位于 bg 中,则此行不起作用。

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

不工作

edit2:我检查了文档

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秒)。

edit3:奇怪的是,即使浏览器窗口最小化,相同的代码在具有 selenium 2.39 python 2.6.6 firefox 28 centos 6.4 的计算机上也可以正常工作。但相同的代码尝试另外两台机器失败,浏览器窗口必须最大化并且按钮必须“可见” A、win7 x64 火狐 28 硒 2.41 python2.7 B.Fedora 20 firefox 28 selenium 2.41 python2.7

python firefox selenium xpath webdriver
9个回答
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


7
投票

要回答您原来的问题,您需要使用最大化窗口方法让浏览器重新获得焦点并置于前台。 在 Python 中应该是这样的:

browser.maximize_window()

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;
}

3
投票

现在我不使用 python,但是我在 Groovy 中解决这个问题的方法如下:

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

我调用了 javascript

alert
函数,该函数将窗口带到前台,然后我用
webdriver.switchTo().alert().accept()
消除了警报。希望 Python 中也有类似的东西。

希望这有帮助!


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
投票

这是一个完整的 hack,但它对我在 Windows 7 中使用 IE、selenium 远程驱动程序有效。

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

我发送一个空白页面,然后最小化和最大化两次。这似乎导致新创建的 IE 具有前台焦点。


3
投票

Python 3.9.8 和 Chrome 106:

def bring_to_front():
    position = driver.get_window_position()
    driver.minimize_window()
    driver.set_window_position(position['x'], position['y'])

1
投票

开箱即用,selenium 不会公开驱动程序进程 id 或浏览器 hwnd,但这是可能的。 下面是获取hwnd的逻辑

  • 驱动程序初始化后,获取集线器的 url 并提取端口号
  • 从端口号中找到正在使用该端口监听的进程id,即:驱动程序的PID
  • 导航后,从 iexplore 的所有实例中找到与驱动程序 pid 匹配的父 PID,即浏览器 pid。
  • 获取浏览器pid的Hwnd 一旦找到浏览器 hwnd,您就可以使用 win32 api 将 selenium 带到前台。

不可能在这里发布完整的代码,将浏览器置于前面的完整工作解决方案(C#)在我的博客上

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


0
投票

这对我也有用(Python 3.8.0、chromedriver 和 Windows 10):

ws = driver.get_window_size()
driver.minimize_window()
driver.set_window_size(ws['width'], ws['height'])
© www.soinside.com 2019 - 2024. All rights reserved.