前言
当selenium升级到4.7后使用远程调用服务,无法上传文件。
driver = webdriver.Remote("http://127.0.0.1:9515", options=get_chrome_options())
使用本地浏览器启动时,上传文件正常:
driver = webdriver.Chrome(
executable_path=r'C:\Users\Desktop\chromedriver.exe',
options=get_chrome_options()
)
发现问题
当使用本地浏览器执行时和远程服务调用走的send_keys逻辑不一样导致(以下是源码send_keys方法)。
解决问题
调试代码时没有思路,随后在github中找到相似的问题(https://github.com/SeleniumHQ/selenium/issues/10047)。
具体描述:
作者给出的方案:
目前没太能理解这句话的意思,只能用简单粗暴的方法,在send_keys执行的时候将driver._is_remote=False。
def upload(self):
self.driver._is_remote = False
ele = self._get_element()
ele.send_keys(file_path)
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理