硒文件上传使文件选择器窗口打开(OS / X和Python) [英] Selenium file upload leaves file selector window open (OS/X and Python)

查看:203
本文介绍了硒文件上传使文件选择器窗口打开(OS / X和Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Selenium将文件上传到远程服务器,但文件选择器对话框即使在文件上传后仍然存在。 这个答案我的线索我有以下代码:

从selenium导入contextlib,time 
导入webdriver
导入selenium.webdriver.common.action_chains as action_chains
with contextlib.closing(webdriver.Chrome() )作为驱动程序:
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(驱动程序).click(file_upload).perform()#https://stackoverflow.com/ a / 16864547/2829764

但是最后文件上传窗口仍然存在,我怀疑因为我需要一个稍微不同的解决方法在Mac上。任何人都可以帮忙?

解决方案

不要点击上传按钮。



通过 send_keys()设置文件名,然后点击Go(已测试过并适用于我):

<$ ($ / $ path / to / file.jpeg)
driver.find_element_by_css_selector('div#点击()


I'm able to upload a file to a remote server using Selenium, but the file selector dialog sticks around even once the file is uploaded. 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 I have the following code:

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() # https://stackoverflow.com/a/16864547/2829764

But at the end of this the file upload window is still there. I suspect I need a slightly different workaround since I'm on a Mac. Can anyone help?

解决方案

Don't click upload button at all.

Set the filename via send_keys() and click "Go" (tested and works for me):

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()

这篇关于硒文件上传使文件选择器窗口打开(OS / X和Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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