如何解决以无头模式上传文件
以无头模式运行硒。打开浏览器后,第一页将加载一个文件页面,单击该按钮后,将打开“上传文件”对话框。如何以无头模式上传文件?在无头模式下运行此代码时,它可以工作,但在无头模式下,它在top_windows中找不到“打开”,这意味着打开文件对话框未打开:
def load_image(self,file_path):
gui_test_lib = BuiltIn().get_library_instance('GuiTestLibrary')
autoid_plus_button = gui_test_lib.project_mapping['load']['load_image_plus']['AutomationId']
element_button = BaseElement(f"id:{autoid_plus_button}")
parent_element = element_button.element.get_webelement(element_button.locator).find_element_by_xpath("..")
button = parent_element.find_element_by_class_name("MuiButtonBase-root")
logger.info(f"click button: {gui_test_lib.project_mapping['load']['load_image_plus']['Text']}")
current_directory_path = os.path.join(os.path.abspath(os.getcwd()),Path(file_path))
button.click()
time.sleep(2)
self.handle_upload_file_dialog(f'{current_directory_path}')
time.sleep (5)
self._verify_image_loaded()
def handle_upload_file_dialog(self,file_path):
sleep = 3
windowsShell = comclt.Dispatch("WScript.Shell")
time.sleep(sleep)
results = []
top_windows = []
win32gui.EnumWindows(self.windowEnumerationHandler,top_windows)
for i in top_windows:
if "open" in i[1].lower():
win32gui.ShowWindow(i[0],5)
time.sleep(sleep)
windowsShell.SendKeys(file_path)
time.sleep(sleep)
windowsShell.SendKeys("{TAB}{TAB}")
time.sleep(sleep)
windowsShell.SendKeys("{ENTER}")
break
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。