1. 程式人生 > 實用技巧 >檔案上傳+滾動條操作+執行js程式碼

檔案上傳+滾動條操作+執行js程式碼

1、檔案上傳2中場景 直接send_keys(file_path) 第二種通過三方呼叫(呼叫瀏覽器之外的系統需要進行等待time.sleep())

from pywinauto import Desktop
app = Desktop()
dialog = app['開啟']    #根據名字找到彈出視窗
dialog["Edit"].type_keys(file_path)     # 在輸入框中輸入值
dialog["Button"].click()
#linux系統下
import pyautogui
import pyperclip

pyperclip.copy(file_path)

time.sleep(2)
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter', presses=2)

time.sleep(2)

2、滾動條

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://readhub.cn/topics")

# 滑動到頁面最底部

for i in range(5):
    js_code = 'window.scrollTo(0, document.body.scrollHeight);'
    driver.execute_script(js_code)

time.sleep(3)

  

"""滾動條滾動到當前元素"""
# 先定位 常見問題
el = driver.find_element_by_link_text("常見問題")
# 把 常見問題的元素 滑動的可見範圍內
el.location_once_scrolled_into_view

 3、執行js程式碼

e = driver.find_element_by_id("train_date")

# 傳送 js code 給瀏覽器
js_code = """e = document.getElementById("train_date");"""
driver.execute_script(js_code)

time.sleep(0.2)
js_code = """e.readOnly = false;"""
driver.execute_script(js_code)

time.sleep(0.2)
js_code = """e.value = "2020-07-20";"""
driver.execute_script(js_code)