启动应用
# 设置谷歌驱动器
driver_path = r'driver\chromedriver.exe'
service = webdriver.chrome.service.Service(driver_path)
service.start()
#以下是必要设置,否则启动不了或获取不到electron应用
options.add_argument('--disable-gpu') # fix:DevToolsActivePort file doesn't exist
options.add_argument('--disable-dev-shm-usage') # fix:DevToolsActivePort file doesn't exist
options.add_argument('--remote-debugging-port=9222') # fix:DevToolsActivePort file doesn't
options.binary_location = f"C:\Program Files (x86)\CtyunClouddeskUniversal\CtyunClouddeskUniversal.exe"
#设置electron应用的绝对路径
options.binary_location = f"xxx.exe"
pc_driver = webdriver.Chrome(service=service, options=options)
xpath获取元素,实现登录
user_input = pc_driver.find_elements(By.XPATH, "xxxxx")[0]
user_input.clear()
user_input.send_keys(acc)
pwd_input = pc_driver.find_elements(By.XPATH, "xxxxx")[0]
pwd_input.clear()
pwd_input.send_keys(pwd)
login_btn = pc_driver.find_elements(By.XPATH, "xxxxx')]")[0]
login_btn.click()
如果electron应用有多个窗口,注意要切换窗口,再继续操作,否则会获取不到对应元素
all_handles = pc_driver.window_handles
pc_driver.switch_to.window(all_handles[0])