现在我正在尝试使用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)
答案 0 :(得分:1)
这将使用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