Selenium 在使用 selenium grid 在 node 节点运行时,对于非 input 框文件上传操作无效

嘉赫 · 2024年03月14日 · 最后由 清白之年 回复于 2024年03月29日 · 4694 次阅读

def upload_file_other(self, locator, file_path, flag=False, is_sleep=True):
"""文件上传 - 非 input 类型"""
self.driver._is_remote = False
ele = self.find_element(locator)
self.highLightElement(ele)
if flag:
webdriver.ActionChains(self.driver).move_to_element(ele).perform()
self.driver.execute_script("arguments[0].click();", ele)
else:
webdriver.ActionChains(self.driver).move_to_element(ele).click(ele).perform()
t.md_logger().info(f"点击元素:{locator}")
file_path = util.file_path(file_path)
common.upload_file(file_path)
t.md_logger().info(f"选择本地文件:{file_path}")
if is_sleep:
time.sleep(1)

def upload_file_gui(self, locator, file_path):
self.driver._is_remote = False
ele = self.find_element(locator)
webdriver.ActionChains(self.driver).move_to_element(ele).click(ele).perform()
time.sleep(2)
file_path = util.file_path(file_path)
pyautogui.write(file_path)
time.sleep(2)
pyautogui.press('enter')
time.sleep(2)
pyautogui.press('enter')

def upload_file_autoit(self, locator, file_paths):
ele = self.find_element(locator)
webdriver.ActionChains(self.driver).move_to_element(ele).click(ele).perform()
time.sleep(2)
file_paths = util.file_path(file_paths)
file_path = util.file_path_upload()
try:
if os.path.exists(file_path):
os.system('%s %s' % (file_path, file_paths))
except Exception as e:
raise e

这个是采用的三种方式,均能在 Selenium Hub 上传成功,node 节点运行时,对于非 input 框文件上传操作无效

共收到 5 条回复 时间 点赞

具体报什么异常没有
node 上有你需要上传的文件吗

其实我想请教下,在什么情况下才会需要使用 arguments[0].click();

llei623 回复

这个没有具体限制吧?selenium 执行定位元素操作底层也是用的 js 命令?

fiskeryang 回复

点击上传图片后,弹出系统文件窗口,这个在子节点机器上就定位不到了

嘉赫 回复

因为弹窗的框是操作系统文件弹框不是浏览器弹框, 我解决过相同的问题,主要思路就是不要让这个系统弹窗弹出一旦弹出自动化就进行不下去了,我上次解决是看了这段代码,你可以参考下。 https://gist.github.com/florentbr/349b1ab024ca9f3de56e6bf8af2ac69e

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册