在python中通过sendkeys上传文件打开文件选择器弹出窗口

时间:2018-03-01 07:25:55

标签: python selenium firefox sendkeys headless

需要自动化的组件是antd - upload。

https://ant.design/components/upload/ --> Can be found here

上传按钮可见,但输入"标记"不可见:

组件视图:This is the button how it is seen on the screen

HTML视图:This is the view of the inspection of the component

当你看到"输入"不可见。

让selenium与输入交互(使用sendkeys)我需要在屏幕上显示输入元素。

我按如下方式制作执行脚本:

file_input = self.driver.find_element_by_xpath(xpath)
self.driver.execute_script('arguments[0].style.display = "block"; ', file_input)

在此之后,我得到了组件的以下视图:component ui view

html视图:HTML view

在此之后,我通过向元素发送键来与组件交互:

file_input.send_keys(fpath)

在这一步我发生了两件事: 1. 显示文件选择器弹出窗口 2. 该文件由send_keys上传。

当我尝试在无头浏览器中运行脚本时,这会导致出现问题。显示的消息:

SessionNotCreatedException: Message: Tried to run command without establishing a connection headless browser

这只是因为在无头浏览器模式下运行。显示浏览器时,脚本将继续运行。我也尝试在显示和无头浏览器上使用模块pyautogui:

pyautogui.keyDown('esc')
pyautogui.keyUp('esc')

这仅对显示的浏览器有帮助,因此弹出窗口已关闭。但对于无头浏览器,这并没有帮助。

我在MacOS Sierra,Firefox(58.0.2)上运行无头浏览器选项,python 2.7,selenium 3.8

如果有人知道如何解决这个问题,我将非常感激。

谢谢

1 个答案:

答案 0 :(得分:0)

使用最新的chromedriver和chrome进行更新。当通过selenium中的send-keys运行此步骤时,无法使用无头浏览器弹出窗口。

Firefox仍然存在geckodriver的问题。