1、上传文件

from selenium import webdriver
import time
import os driver = webdriver.Chrome()
src_file = "file:///" + os.path.abspath("test.html") driver.get(src_file) while True:
ele = driver.find_element_by_name("selenium_upload").send_keys(os.path.abspath("test.html"))
time.sleep(5) driver.close()

  

2、使用chrom浏览器器实现下载操作

# chrom浏览器实现下载操作
from selenium import webdriver from time import sleep
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'e:\\'}
# profile.default_content_settings.popups:禁止弹出窗口
# download.default_directory:设置下载路径
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome()
driver.get('https://pypi.org/project/selenium/#files')
driver.find_element_by_link_text('selenium-3.13.0.tar.gz').click()
sleep(13)
driver.quit()

  

3、操作cookies

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.cnblogs.com/")

# 1、获取所有的cookies信息
print(driver.get_cookies()) driver.add_cookie(
{
"name":"dsb",
"value":"xsb"
}
) print(driver.get_cookies()) # 通过name属性删除单个cookie
driver.delete_cookie("dsb") # 通过name属性查看单个cookie的信息
print(driver.get_cookie("dsb")) # 删除所有的cookies
driver.delete_all_cookies()
print(driver.get_cookies()) driver.close()

  

4、操作滚动条

操作滚动条
from selenium import webdriver
driver = webdriver.Chrome() driver.get("https://www.baidu.com") driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click() # top = "var q=document.documentElement.scrollTop={height}".format(height=10000)
top="var q=document.body.scrollTop=10000"
# middle = "var q=document.documentElement.scrollTop={height}".format(height=0)
middle="var q=document.body.scrollTop=0" driver.execute_script(top)
import time
time.sleep(5) driver.execute_script(middle)
time.sleep(5)
driver.close()

  

5、截图

selenium实现截图
from selenium import webdriver driver = webdriver.Chrome()
driver.maximize_window() driver.get("https://www.baidu.com") try:
driver.find_element_by_id("kw").send_keys("selenium")
except Exception as e:
print(e)
else:
driver.get_screenshot_as_file("kw.png") finally:
driver.close()

  

05-11 15:23