gpt4 book ai didi

python - 如何将selenium浏览器带到前台?

转载 作者:太空狗 更新时间:2023-10-30 00:29:51 25 4
gpt4 key购买 nike

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

看来这个是有效的。以前我尝试完全消除这种情况,但它不起作用可能只是因为我没有“等待”足够长的时间( sleep 5 秒)。

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

最佳答案

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

关于python - 如何将selenium浏览器带到前台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com