首页 > 解决方案 > 如何获取属性等到Firefox Selenium,python,window,版本:67.0(64位)+ selenium 3.9.0中的状态变化

问题描述

想确认数据下载是否完成。

目标属性信息如下:

<richlistitem  class="download download-state" active="true" 
orient="horizontal" 
onclick="DownloadsView.onDownloadClick(event);" state="1" 
exists="true" selected="true">....<description 
class="downloadDetails downloadDetailsHover" crop="end" 
value="49.5 KB — xxx.com:8080 — 8:09 PM"/><description 
class="downloadDetails downloadDetailsButtonHover" 
crop="end"/></vbox></hbox><toolbarseparator/><button 
class="downloadButton downloadIconShow" 
oncommand="DownloadsView.onDownloadButton(event);" 
tooltiptext="Open Containing Folder"/></richlistitem>

# Current script:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

download_CSV =dr.find_element_by_id('xxxx')
download_CSV.click()
NEW_TAB = driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
cur_windows = driver.window_handles # NEW TAB

time.sleep(2)

driver.switch_to.window(cur_windows[1])
driver.get('about:downloads')
# EC.until the download message attribute change script

如何获取state="1"的属性(1=表示下载完成,0=还没有)或者exist="true",需要等到state="1"从"0"/orexist="true" “ 出现 ? (欢迎使用其他方法定义文件已经完成下载)

无法将此答案应用于我的情况。 Selenium:等到属性值改变

标签: pythonseleniumfirefoxattributeswebdriverwait

解决方案


我不知道这是否可行,但也许您可以尝试使用预期条件:presence_of_element_located

如果网站中没有其他元素响应 Xpath 定位器

//richlistitem[@state='1']

也许这可以解决您的问题:

WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//richlistitem[@state="1"]')))

此代码将在 10 秒内查找元素“//richlistitem[@state='1']”,直到该元素位于 DOM 上,否则将返回 TimeoutException


推荐阅读