Shu*_*man 5 python firefox selenium xpath webdriver
如果浏览器窗口在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()
Run Code Online (Sandbox Code Playgroud)
它出错了
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: ''
Run Code Online (Sandbox Code Playgroud)
但如果我将浏览器窗口留在前面,它就不会出错.是因为我不应该使用
EC.element_to_be_clickable?我试图忽略错误并继续单击我想要的按钮,但它说如果浏览器窗口在后台它找不到按钮.所以我认为我应该做的是在它之前,我把浏览器窗口带到前台?
我看到有人在讨论 switchTo()?但是我的浏览器对象没有那种方法.我该怎么做才能将窗口独立地带到前端系统?谢谢
测试系统
python 2.7 windows 7 x64
python 2.6.6 CentOS 6.4
编辑:我试过了
browser.execute_script("window.focus();")
Run Code Online (Sandbox Code Playgroud)
和
# 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()
Run Code Online (Sandbox Code Playgroud)
不工作
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.
Run Code Online (Sandbox Code Playgroud)
似乎因为当窗口处于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
Run Code Online (Sandbox Code Playgroud)
看起来这个工作正常.以前我尝试完全删除条件,它不起作用可能只是因为我没有"等待"很长时间(睡眠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
这对我有用,可以将浏览器窗口置于前台。我在 Python 3 上的 Selenium 3.6.0 上使用 chromedriver 进行了测试。
from selenium import webdriver
driver = webdriver.Chrome()
driver.switch_to.window(driver.current_window_handle)
Run Code Online (Sandbox Code Playgroud)
Java 中的相关答案 - https://sqa.stackexchange.com/questions/16428/how-can-i-bring-chrome-browser-to-focus-when-running-a-selenium-test-using-chrom
要回答原始问题,您需要使用最大化窗口方法让浏览器重新获得焦点并置于前台.在Python中它应该是这样的:
browser.maximize_window()
Run Code Online (Sandbox Code Playgroud)