Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在“selenium webdriver”中关闭文件资源管理器`_Python_Selenium - Fatal编程技术网

Python 如何在“selenium webdriver”中关闭文件资源管理器`

Python 如何在“selenium webdriver”中关闭文件资源管理器`,python,selenium,Python,Selenium,现在我正在尝试使用selenium webdriver上传文件 我能够成功上载文件,但是,我无法使用esc关闭文件资源管理器。 我使用了switch_to方法切换窗口,然后尝试关闭它,但它也不起作用。 在selenium webdriver中是否有关闭文件资源管理器的方法 下面是我的代码 docs = [ 'DriversLicense', 'CarRegistration', 'Insurance', 'BizCertificate' ] tester_dir_path =

现在我正在尝试使用selenium webdriver上传文件

我能够成功上载文件,但是,我无法使用
esc
关闭文件资源管理器。 我使用了
switch_to
方法切换窗口,然后尝试关闭它,但它也不起作用。 在selenium webdriver中是否有关闭文件资源管理器的方法

下面是我的代码

docs = [
  'DriversLicense',
  'CarRegistration',
  'Insurance',
  'BizCertificate'
]

tester_dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
source_dir_path = os.path.join(tester_dir_path, 'source')
driver_dir_path = os.path.join(source_dir_path, 'driver')

for idx, doc in enumerate(docs):
  path = os.path.join(driver_dir_path, f"{str( int(idx) + 1 )}.jpg")

  doc_ele = self.driver.find_element_by_css_selector('ul.file-list li[data-prefix="' + doc + '"]')
  doc_ele.click()

  time.sleep(1)
  self.driver.switch_to.window(self.driver.window_handles[-1])
  webdriver.ActionChains(self.driver).send_keys(Keys.ESCAPE).perform()

  file_input = self.driver.find_element_by_css_selector('ul.file-list li[data-prefix="' + doc + '"] input[name="driver-doc"]')
  file_input.send_keys(path)

  time.sleep(5)

这将关闭使用python和Selenium WebDriver的任何当前窗口

步骤1:安装并导入pyatspi2软件包

步骤2:在代码中使用此函数

import pyatspi
def CloseWindow():

pyatspi.Registry.generateKeyboardEvent(64, None, pyatspi.KEY_PRESS)
pyatspi.Registry.generateKeyboardEvent(70, None, pyatspi.KEY_PRESSRELEASE)
pyatspi.Registry.generateKeyboardEvent(64, None, pyatspi.KEY_RELEASE)
步骤3:在要关闭附件窗口或任何窗口的位置调用此函数

attachbtn = driver.find_element_by_id(":nx")
attachbtn.click() 

driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("file path")

CloseWindow() #it will call the function and close the currently opened window

什么是“文件资源管理器”?您可以共享相同的html部分吗?为什么要关闭文件资源管理器?