如何将硒浏览器带到前端? [英] How to bring selenium browser to the front?

查看:55
本文介绍了如何将硒浏览器带到前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

if the browser window is in bg, then this line doesn't work.

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?我尝试忽略该错误并继续单击我想要的按钮,但它说如果浏览器窗口在后台,则找不到该按钮.所以我认为我应该做的是在到达那行之前,将浏览器窗口置于前台?

but if i leave my browser window in front, it doesn't error out. is it because i should not use EC.element_to_be_clickable ? i tried ignoring the error and continue to click the button i want, but it says it can not find the button if the browser window is in background. so i think what i should do is before it comes to that line, i bring the browser window to foreground ?

我看到有人在讨论switchTo()吗?但我的浏览器对象没有该方法.我该怎么做才能将窗户独立放置在前台系统上?谢谢

i saw some people discussing switchTo() ? but my browser object doesn't have that method. what can i do to bring the window to front system independently? thanks

测试系统

python 2.7 Windows 7 x64

python 2.7 windows 7 x64

python 2.6.6 CentOS 6.4

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:我检查了文档

edit2: i checked the doc

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或最小化状态时,该元素不是可见的",所以永远不会满足此条件? 无论如何,我尝试了另一种条件

it seems because when the window is in bg or minimized,the element is not "visible", so this condition is never met ? anyway i tried another condition

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

it seems this one is working. previously i tried completely removing the condition and it doesn't work maybe simply because i didn't "wait" long (sleep 5 sec ) enough.

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

edit3: strangely the same code works fine on a computer with selenium 2.39 python 2.6.6 firefox 28 centos 6.4 even with browser window minimized. but same code tried another two machines failed, browser window has to be maximized and button has to be "visible" A. win7 x64 firefox 28 selenium 2.41 python2.7 B. Fedora 20 firefox 28 selenium 2.41 python2.7

推荐答案

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

To answer your original question, you need to use the maximize window method for the browser to regain focus and be placed in foreground. it should be something like this in Python:

browser.maximize_window()

这篇关于如何将硒浏览器带到前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆