在使用 selenium grid 在 node 節點執行時,對於非 input 框檔案上傳操作無效

嘉赫發表於2024-03-14

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 框檔案上傳操作無效

相關文章