如何让 selenium 浏览器走到前列?

5kgi1eie  于 2022-11-10  发布在  其他
关注(0)|答案(8)|浏览(446)

如果浏览器窗口为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()?但我的浏览器对象没有该方法。我可以做些什么来独立地将窗口带到前台系统?谢谢
测试系统
Python2.7 Windows 7 x64
Python2.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.

这似乎是因为当窗口处于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秒)。
编辑3:奇怪的是,即使在浏览器窗口最小化的情况下,同样的代码也能在Selens2.39、Python2.6.6、Firefox 28 Centos 6.4的电脑上运行得很好。但同样的代码尝试了另外两台机器都失败了,浏览器窗口必须最大化,按钮必须“可见”A。Win7 x64 Firefox 28 Selence 2.41 python2.7 B.Fedora 20 Firefox 28 Selence 2.41 python2.7

zpf6vheq

zpf6vheq1#

这对我将浏览器窗口带到前台起到了作用。我在Python3上的Selens3.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

vmdwslir

vmdwslir2#

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

browser.maximize_window()
46scxncf

46scxncf3#

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

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

我调用了将窗口带到前台的Java脚本alert函数,然后使用webdriver.switchTo().alert().accept()解除了警报。希望在Python中也有类似的东西。
希望这个能帮上忙!

8wtpewkr

8wtpewkr4#

已在Python36中测试并运行

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

9w11ddsr

9w11ddsr5#

这是一个彻头彻尾的破解,但它在我使用IE时有效,这是Windows7中的Selify远程驱动程序。

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

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

hkmswyz6

hkmswyz66#

在C#中,使用

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

没有将窗口带到前面,而是只将它放在其他窗口中,让它处于Z顺序。
起作用的是以下代码:

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

y0u0uwnf7#

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

  • 驱动初始化后,获取集线器的url并提取端口号
  • 从端口号中,找到正在使用此端口进行侦听的进程ID,即。驱动程序的PID
  • 导航后,从iExplore的所有示例中查找父ID与驱动程序的ID匹配,即浏览器ID。
  • 获取浏览器id的hwnd一旦找到浏览器hwnd,就可以使用Win32 API将Selence带到前台。

不可能在这里发布完整的代码,使浏览器领先的完整工作解决方案(C#)在我的博客上
http://www.pixytech.com/rajnish/2016/09/selenium-webdriver-get-browser-hwnd/

vlju58qv

vlju58qv8#

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'])

相关问题