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

问题描述 投票:0回答:1

需要自动化的组件是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

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

谢谢

python selenium firefox sendkeys headless
1个回答
0
投票

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

Firefox仍然存在geckodriver这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.