selenium中的webdriver一些常用操作
from selenium import webdriver
import time
#引入鼠标操作
from selenium.webdriver.common.action_chains import ActionChains
login_url = '你要登陆的地址'
Chrome_address='C:\chromedriver.exe'
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless') #无头浏览器
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--disable-gpu') #禁用gpu
drivers = webdriver.Chrome(Chrome_address, chrome_options=chrome_options)
user_name='admin'
pass_word='123456'
Button_Xpath=''
Close_Xpath=''
drivers.get(url=login_url)
# 最大化
drivers.maximize_window()
time.sleep(3)#等待3秒
drivers.find_element_by_name(name="username").clear()#用户名输入框清空
drivers.find_element_by_name(name="username").send_keys(user_name)#输入准备好的用户名
drivers.find_element_by_name(name="password").clear()#密码框清空
drivers.find_element_by_name(name="password").send_keys(pass_word)#输入准备好的密码
ac = ActionChains(drivers)#实例化drivers,方便下面使用
button_ele = drivers.find_element_by_xpath(Button_Xpath) # 提交弹窗的xpath表达式
ac.click(button_ele).perform() # 鼠标移动到提交按钮,左键点击
time.sleep(3)#等待3秒
close_ele = drivers.find_element_by_xpath(Close_Xpath) # 关闭弹窗的xpath表达式
#ac.move_to_element(close_ele).perforn() # 鼠标移动悬浮到关闭按钮上面
ac.move_to_element(close_ele).click().perform() # 鼠标移动到关闭按钮,并且点击
#关闭
drivers.close()