【问题标题】:How to bring selenium browser to the front?如何将 selenium 浏览器带到最前面?
【发布时间】:2014-04-21 15:15:10
【问题描述】:

如果浏览器窗口在 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.

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

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 firefox 28 selenium 2.41 python2.7 B. Fedora 20 firefox 28 selenium 2.41 python2.7

【问题讨论】:

  • 在点击之前可以尝试move_to_element()吗?
  • @alecxe 但它已经在EC.element_to_be_clickable 行出错了(在我点击它之前),元素是无,我怎么能在不引用它的情况下移动到它?该页面使用了一些ajax调用,我必须等待按钮出现,但只要它在bg中,无论我等待多长时间,它都不会“出现”到webdriver,实际上它出现在浏览器窗口中,我可以看到它。
  • 去掉EC.element_to_be_clickable检查并尝试move_to_element()
  • @alecxe 你的意思是这样吗?见问题编辑。哦等等如何访问这个 move_to_element() 方法?它不在浏览器对象中。

标签: python firefox selenium xpath webdriver


【解决方案1】:

在 C# 中,使用

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

没有将窗口拉到前面,而是仅将其集中在其他窗口中,使其按 Z 顺序排列。

下面的代码起作用了:

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

【讨论】:

    【解决方案2】:

    这对我来说可以将浏览器窗口置于前台。我在 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

    【讨论】:

    • 对于 Python3:driver.switch_to.window(driver.current_window_handle)
    • @etayluz 谢谢——我已经更新了答案以匹配。看起来旧方法已被弃用。
    【解决方案3】:

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

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

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

    【讨论】:

    • 感觉很老套,但比其他任何方法都好用
    【解决方案4】:

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

    browser.maximize_window()
    

    【讨论】:

    • 我在 windows 7 x64 上试过这个,firefox windows 在后台最大化,我仍然得到错误
    • 这很奇怪。这将永远把它带到我面前。当您尝试我的建议时,我假设您的代码看起来像这样?尝试:browser.locateElement(x) 除外:browser.maximize_window() browser.locateElement(x) 另外,出于好奇,你还有什么正在运行,为什么你不能让你的浏览器 100% 前台访问(例如通过给它一个专用的虚拟机)
    • 普通用户只有一个屏幕,其他工作需要有一个普通的firefox窗口。不应该使用 selenium firefox。大多数用户在 Windows 上,如何设置虚拟机?对于`browser.locateElement(x)`,我如何首先获得对元素x的引用?通过使用 xpath ? (但这假设 x 已经存在,这可能不是因为它是由 ajax 加载的,但我可以等待足够长的时间让它显示)
    • 我没有理解你所说的用户只有一个屏幕的意思。您的公司应该专门为您的测试提供单独的台机器,无论是物理的还是虚拟的(使用Virtualbox)。
    • 我尝试在 macOS 上使用 Chrome 驱动程序通过 Python 使用 Selenium,但它对窗口没有任何影响。
    【解决方案5】:

    在 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

    【讨论】:

      【解决方案6】:

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

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

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

      希望这会有所帮助!

      【讨论】:

        【解决方案7】:

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

        • 驱动初始化时,获取hub的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/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-02
          • 2012-01-16
          • 2018-04-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多