Selenium 文件上传使文件选择器 window 打开(OS/X 和 Python)

Selenium file upload leaves file selector window open (OS/X and Python)

我可以使用 Selenium 将文件上传到远程服务器,但文件选择器对话框在文件上传后仍然存在。 The Selenium FAQ notes that, "You can't interact with the native OS file browser dialog directly, but we do some magic so that...." Given the use of "magic" here it's not surprising that the behavior I get is a little rough around the edges. But there appear to be workarounds. Taking my cues from this answer 我有以下代码:

import contextlib, time
from selenium import webdriver
import selenium.webdriver.common.action_chains as action_chains
with contextlib.closing(webdriver.Chrome()) as driver:
    driver.get("http://www.bing.com/images")
    driver.find_element_by_id("sbi_t").click()
    driver.find_element_by_id("sbi_file").click()
    driver.find_element_by_id("sbi_file_upload").send_keys("//Loch Ness Monster.jpg")
    print driver.current_url # Still `http://www.bing.com/images` :(
    file_upload = driver.find_element_by_id("sbi_file_upload")
    action_chains.ActionChains(driver).click(file_upload).perform() # 

但在此结束时,文件上传 window 仍然存在。我怀疑我需要一个稍微不同的解决方法,因为我在 Mac。有人可以帮忙吗?

根本不要点击上传按钮。

通过 send_keys() 设置文件名并单击 "Go"(已测试并适用于我):

element = driver.find_element_by_id("sbi_file_upload")
element.send_keys('/Path/to/file.jpeg')
driver.find_element_by_css_selector('div#sbi_sb_ipt span[name=go]').click()