Selenium + Python - 点击按钮后,如何抓住弹出窗口(特别是弹出上传图片)

时间:2015-05-28 13:48:58

标签: python selenium selenium-webdriver

我正在尝试将图片上传到Microsoft的http://how-old.net/ API,以便与我们的年龄和性别分类算法[CVPR AMFG15]进行比较。

我正在使用Selenium,它很容易导航到网站并点击“使用你自己的照片”按钮:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://how-old.net/")

elem=driver.find_element_by_id("uploadFileId")
elem.click()

我尝试了在网上找到的各种解决方案:

upload_window=driver.find_element_by_partial_link_text("File")

或者:

driver.SwitchTo().defaultContent();

似乎没什么用。再一次,找到按钮并单击它非常容易,抓住窗口并上传图像似乎很难。

编辑:

我也尝试了以下内容:

driver = webdriver.Firefox()
driver.get("http://how-old.net/")
file_input=driver.find_element_by_id("uploadBtn")
driver.execute_script("arguments[0].style.visibility='visible';", file_input)
file_input.send_keys('image_name.jpg')

但我得到以下例外:

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:

即使从我看到的内容,按钮仍然可见(请参阅随附的打印屏幕)。

enter image description here

[CVPR AMFG15] Levi,Gil和Tal Hassner。 “使用卷积神经网络的年龄和性别分类。”

1 个答案:

答案 0 :(得分:2)

您无法使用file控制文件上传窗口。

解决问题的常用方法是避免首先打开它。

找到send_keys()输入并使用file_input = driver.find_element_by_id("uploadBtn") file_input.send_keys("/absolute/path/to/file")

将路径传递给文件
driver.execute_script("arguments[0].style = {visibility: 'visible'};", file_input)

这不会“按原样”起作用,因为隐藏了文件输入,使其首先显示

tell application Finder
    mount volume \\server\share
    mount volume \\server\share
    mount volume \\server\share
end tell
相关问题