一、日期控件

只读控件的日期控件,如何实现输入?
javascript 实现 调用execute_script

  1. """
  2. js操作
  3. execute_script
  4. """
  5. import time
  6. from selenium import webdriver
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.common.keys import Keys
  9. driver=webdriver.Chrome()
  10. driver.get("https://www.12306.cn/index/")
  11. time.sleep(2)
  12. # 只读日期控件元素
  13. el_data=driver.find_element(By.ID,"train_date")
  14. # 去掉readonly属性‐‐‐》通过js操作元素driver.execute_script()
  15. # arguments 把execute_script后面的第二个或者第三个或者到第n个参数到放在arguments
  16. js="var el=arguments[0];el.removeAttribute('readonly');"
  17. # 执行js脚本,去掉readonly属性
  18. driver.execute_script(js,el_data)
  19. # 清除日期控件默认值
  20. el_data.clear()
  21. time.sleep(2)
  22. # # 日期控件输入设置的日期
  23. el_data.send_keys('2021‐10‐01')
  24. print("时间控件设置的日期:",el_data.get_attribute("value"))

二、滚动条操作

什么情况下,需要把元素放到可见区域我才可以操作呢?
懒加载/慢加载 必须把元素移动可见区域,才会实现加载 ——》滚动条操作
execute_script

  1. """
  2. 滚动条操作
  3. """
  4. import time
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver.support.wait import WebDriverWait
  9. from selenium.webdriver.support import expected_conditions as EC
  10. driver=webdriver.Chrome()
  11. driver.get("https://www.baidu.com")
  12. driver.find_element(By.ID,"kw").send_keys("selenium webdriver")
  13. driver.find_element(By.ID,"su").click()
  14. loc=(By.XPATH,"//div[@id='7']/h3/a")
  15. # 显示等待 presence_of_element_located不一定元素可见,只要存在即可
  16. WebDriverWait(driver,15).until(EC.presence_of_element_located(loc))
  17. el2=driver.find_element(*loc)
  18. time.sleep(2)
  19. # el2.click()
  20. # 实现滚动条从顶部到底部
  21. driver.execute_script("scrollTo(0,document.body.scrollHeight)")
  22. time.sleep(2)
  23. # 实现滚动条从浏览器底部到顶部
  24. driver.execute_script("scrollTo(document.body.scrollHeight,0)")
  25. time.sleep(2)
  26. # 操作哪个元素,滚动条移动到元素附近(元素与页面的顶部对齐,元素与页面的底部对齐)
  27. js2="arguments[0].scrollIntoView(false);"
  28. driver.execute_script(js2,el2)
  29. time.sleep(2)

三、文件上传

  1. """
  2. 文件上传操作
  3. 类型一:支持直接输入
  4. 类型二:不可以直接输入,只能选择
  5. """
  6. import time
  7. from selenium import webdriver
  8. from selenium.webdriver import ActionChains
  9. from selenium.webdriver.common.by import By
  10. # 类型一:支持直接输入
  11. driver=webdriver.Chrome()
  12. driver.get(r"E:\VIP\M211\web\WebCode\class06\test.html")
  13. time.sleep(4)
  14. el_file=driver.find_element(By.ID,"f1")
  15. el_file.send_keys(r"E:\VIP\M211\web\WebCode\class06\test.html")
  16. time.sleep(2)
  17. driver.close()
  18. # 类型二:不可以直接输入,只能选择
  19. # 如何定位windows窗口及窗口的元素?
  20. # 环境准备
  21. # 1、定位工具(识别window窗口及元素)‐‐‐winSpy
  22. # 2、第三方库 pywin32
  23. # 安装: pip install pywin32
  24. # 切换源 pip install ‐i https://pypi.douban.com/simple pywin32 --trust-host pypi.douban.com
  25. # 如何定位windows窗口及窗口的元素?
  26. #文件上传输入框: #32770‐ComboBoxEx32‐ComboBox‐Edit
  27. # 打开按钮:#32770‐Button
  28. import win32gui
  29. import win32con
  30. driver=webdriver.Chrome()
  31. driver.get(r"E:\VIP\M211\web\WebCode\class06\test.html")
  32. time.sleep(4)
  33. el_file=driver.find_element(By.ID,"f1")
  34. ActionChains(driver).move_to_element(el_file).click().perform()
  35. def upload(filePath,browser_type="Chrome"):
  36. if browser_type == "Chrome":
  37. title = "打开"
  38. else:
  39. title = "文件上传"
  40. #32770‐ComboBoxEx32 ‐ComboBox ‐Edit
  41. dialog = win32gui.FindWindow("#32770",title) #一级窗口 ‘打开窗口’
  42. ComboBoxEx32 = win32gui.FindWindowEx(dialog,0,"ComboBoxEx32",None) #二级
  43. ComboBox = win32gui.FindWindowEx(ComboBoxEx32,0,"ComboBox",None) #三级
  44. edit = win32gui.FindWindowEx(ComboBox,0,"Edit",None) #四级
  45. # 32770‐Button
  46. button = win32gui.FindWindowEx(dialog,0,"Button",None) #四级
  47. # 往文件名编辑框中输入文件路径
  48. # 上传操作
  49. win32gui.SendMessage(edit,win32con.WM_SETTEXT,None,filePath)
  50. win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button) #点击打开按钮
  51. filepath=r"E:\VIP\M211\web\WebCode\class06\test.html"
  52. time.sleep(5)
  53. upload(filePath=filepath)
  54. print("文件上传成功")
  55. time.sleep(10)

作业:

熟练对于时间控件、滚动条、及文件上传元素的操作