selenium-python-行为链与cookie操作

行为链

有时候在页面中的操作可能要有很多步,那么这时候可以使用鼠标行为链ActionChains来完成。

比如现在要将鼠标移动到某个元素上并执行点击事件

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver_path = r'E:\vens\chromedriver\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://www.baidu.com')

input_tag = driver.find_element_by_id('kw')
submit_btn = driver.find_element_by_id('su')

actions = ActionChains(driver)
actions.move_to_element(input_tag)  # 移动到某个元素
actions.send_keys_to_element(input_tag, 'python')  # 在某个元素上输入内容
actions.move_to_element(submit_btn)
actions.click()  # 点击
actions.perform()  # 执行

更多的鼠标操作行为:

  1. click_and_hold(element)     点击但不松开
  2. context_click(element)        右键点击
  3. double_click(element)         双击

 cookie操作

# 获取所有的cookie:
for cookie in driver.get_cookies():
    print(cookie)

# 根据cookie的value获取key:
value = driver.get_cookie(key)

# 删除所有的cookie:
driver.delete_all_cookies()

# 删除某个cookie:
driver.delete_cookie(key)

 

你可能感兴趣的:(python3,Selenium,爬虫,python爬虫入门到精通,python,selenium,爬虫)