操作输⼊框:分为两步。 第⼀步:找到这个元素。 第⼆步:使⽤send_keys(value),将数据填充进去 使⽤clear⽅法可以清除输⼊框中的内容
inputTag.clear()
操作checkbox
因为要选中checkbox标签,在⽹⻚中是通过⿏标点击的。因此想要选中 checkbox标签,那么先选中这个标签,然后执⾏click事件
rememberTag = driver.find_element_by_name("rememberMe")
rememberTag.click()
操作按钮
操作按钮有很多种⽅式。⽐如单击、右击、双击等。这⾥讲⼀个最常⽤的。就 是点击。直接调⽤click函数就可以了
inputTag = driver.find_element_by_id('su')
inputTag.click()
选择select select元素不能直接点击。因为点击后还需要选中元素。这时候selenium就专 ⻔为select标签提供了⼀个类selenium.webdriver.support.ui中的Select。将获取 到的元素当成参数传到这个类中,创建这个对象。以后就可以使⽤这个对象进 ⾏选择了。https://www.17sucai.com/boards/53562.html
from selenium.webdriver.support.ui import Select
有时候在⻚⾯中的操作可能要有很多步,那么这时候可以使⽤⿏标⾏为链类 ActionChains来完成。⽐如现在要将⿏标移动到某个元素上并执⾏点击事件
1 actions = ActionChains(driver)
2 actions.move_to_element(inputTag)
3 actions.send_keys_to_element(inputTag,'python')
4 actions.move_to_element(submitTag)
5 actions.context_click()
6 actions.click(submitTag)
7 actions.perform()
还有更多的⿏标相关的操作
click_and_hold(element):点击但不松开⿏标。
context_click(element):右键点击。
double_click(element):双击。
更多⽅法请参考:http://selenium-python.readthedocs.io/api.html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# cookies = driver.get_cookies() # 返回的是一个列表
#
# # for cookie in cookies:
# #
# # print(cookie)
#
# # 删除cookie # 我先删除 在获取 获取不到了
# driver.delete_cookie('BD_HOME')
#
# print('------------------------------')
#
# print(driver.get_cookie('BD_HOME'))
# time.sleep(2)
# 隐式等待
# driver.implicitly_wait(2)
# driver.find_element_by_id('kwfdfsfsdf').send_keys('python')
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "kw"))
)
time.sleep(2)
finally:
driver.quit()
# try:
# # print(10/0)
# print(10/2)
# except:
# print('出现问题了')
#
# finally:
# print('无论是否出错都会执行该语句')
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# driver.find_element_by_id('su').send_keys('python') # 不能输入内容但是没有报错
# driver.find_element_by_id('head_wrapper').send_keys('python') # 不能输入内容但是报错了
inputTag = driver.find_element_by_id('kw')
inputTag.send_keys('java')
time.sleep(3)
inputTag.clear()
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
# 打开目标网站
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换iframe
driver.switch_to_frame(driver.find_element_by_id('iframe'))
# 找到下拉框
selectTag = Select(driver.find_element_by_class_name('nojs'))
# 选择方式
# 1.根据值来选择
# selectTag.select_by_value('CA')
# 2.根据索引来选择
selectTag.select_by_index(3)
from day10 import password
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
# 打开目标网站 QQ邮箱
driver.get('https://mail.qq.com/')
# 切换iframe
pwd = password.pwd
login_fram = driver.find_element_by_id('login_frame')
driver.switch_to_frame(login_fram)
# 切换登录方式
driver.find_element_by_id('switcher_plogin').click()
# 账号和密码
driver.find_element_by_id('u').send_keys('192149641')
driver.find_element_by_id('p').send_keys(pwd)
# 按钮
driver.find_element_by_id('login_button').click()
from selenium import webdriver
import time
driver = webdriver.Chrome()
# 打开豆瓣
driver.get('https://www.douban.com/')
# 切换iframe
login_frame = driver.find_element_by_xpath('//div[@class="login"]/iframe')
driver.switch_to_frame(login_frame)
time.sleep(2)
# 切换登录方式
driver.find_element_by_xpath('//li[@class="account-tab-account"]').click()
# 选中checkbox选项
driver.find_element_by_id('account-form-remember').click()
# 输入账号密码
driver.find_element_by_id('username').send_keys('xxxxxx')
driver.find_element_by_id('password').send_keys('xxxxxxx')
# 点击登录按钮
driver.find_element_by_class_name('btn-account').click()
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/') # 打开百度
driver.execute_script('window.open("https://www.douban.com/")') # 打开豆瓣
# driver.get('https://www.douban.com/')
# driver.close() # 关闭的是百度
# driver.quit() # 2个都关闭了
driver.find_element_by_id('kw').send_keys('python') # 操作的是百度
print(driver.current_url) # 打印网址 打印的是百度
driver.switch_to_window(driver.window_handles[0])
print(driver.current_url)
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 定位到输入框
inputTag = driver.find_element_by_id('kw')
# 定位到百度按钮
submitBtn = driver.find_element_by_id('su')
# 实例化
actions = ActionChains(driver)
# 把鼠标移动到输入框
actions.move_to_element(inputTag)
# 输入内容
actions.send_keys_to_element(inputTag,'python')
# 点击按钮 百度一下
actions.move_to_element(submitBtn)
actions.click()
# 点击右键
actions.context_click()
# 提交行为链上的操作
actions.perform()