driver.find_element_by_id("kw").send_keys("大道至简")
driver.find_element_by_name("wd").send_keys("大道至简")
driver.find_element_by_class_name("s_ipt").send_keys("大道至简")
driver.find_element_by_xpath("//*[@id='kw']").send_keys("大道至简")
driver.find_element_by_css_selector("input#kw").send_keys("大道至简")
driver.find_element_by_css_selector("form#form>span>input").send_keys("大道至简")
driver.find_elements_by_css_selector("input[type='checkbox']")[0].click()
#导入By类
from selenium.webdriver.common.by import By
对应语法:
By.ID
By.NAME
By.CLASS_NAME
By.LINK_TEXT
By.XPATH
By.CSS_SELECTOR
driver.find_element(By.ID,'kw').send_keys("大道至简")
driver.get("https://www.baidu.com")
el = driver.execute_script('return document.getElementById("kw")')
el.clear()
el.send_keys('Python')
1、value属性定位
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
executable_path = r"G:\BaiduNetdiskDownload\chromedriver.exe"
driver = webdriver.Chrome(executable_path)
driver.get("https://www.chinanews.com/")
driver.maximize_window()
time.sleep(1)
element = driver.find_element(By.ID,"s0")
Select(element).select_by_value("baidu")
2、index属性定位
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
executable_path = r"G:\BaiduNetdiskDownload\chromedriver.exe"
driver = webdriver.Chrome(executable_path)
driver.get("https://news.sina.com.cn/")
# 浏览器最大化
driver.maximize_window()
time.sleep(2)
element = driver.find_element(By.NAME,"time")
time.sleep(1)
Select(element).select_by_index(1)
3、visible_text属性定位
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
executable_path = r"G:\BaiduNetdiskDownload\chromedriver.exe"
driver = webdriver.Chrome(executable_path)
driver.get("https://news.sina.com.cn/")
# 浏览器最大化
driver.maximize_window()
time.sleep(2)
element = driver.find_element(By.NAME,"time")
time.sleep(1)
Select(element).select_by_visible_text("21:00")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
executable_path = r"G:\BaiduNetdiskDownload\chromedriver.exe"
driver = webdriver.Chrome(executable_path)
driver.get("https://news.sina.com.cn/")
# 浏览器最大化
driver.maximize_window()
time.sleep(2)
driver.find_element(By.NAME,"time").find_element(By.XPATH,"//*[@id='blk_syhg_01']/form/select[2]/option[2]").click()
time.sleep(1)
鼠标悬停到标签上后,在Sources中按Ctrl + \ 组合键,进入debug模式,动效JS会停止继续执行
1、鼠标指针悬停实战
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
# 导入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
import time
executable_path = r"G:\BaiduNetdiskDownload\chromedriver.exe"
driver = webdriver.Chrome(executable_path)
# 浏览器最大化
driver.maximize_window()
driver.get("https://www.baidu.com/")
setting = driver.find_element(By.ID,"s-usersetting-top")
ActionChains(driver).move_to_element(setting).perform()
driver.find_element(By.XPATH,"//*[@id='s-user-setting-menu']/div/a[1]").click()
time.sleep(1)
2、鼠标右键实战
driver.get("https://www.baidu.com/")
Baidu = driver.find_element(By.ID,"su")
ActionChains(driver).context_click(Baidu).perform()
time.sleep(1)
3、鼠标双击实战
driver.get("https://www.baidu.com/")
Baidu = driver.find_element(By.ID,"su")
ActionChains(driver).double_click(Baidu).perform()
time.sleep(1)
iframe标签
1、单表单切换实战
driver.get("https://mail.qq.com/")
driver.switch_to.frame("login_frame")
driver.find_element(By.ID,"switcher_plogin").click()
time.sleep(1)
2、嵌套表单切换实战
处理思路:首先切换到第一个iframe中,再切换到第二个iframe标签中,可正常定位;
3、平行表单切换实战
场景:假设当前处在iframe1标签中,需要操作iframe2标签中的元素;
处理思路:首先从当前标签中退出,然后切换到iframe2中操作元素;
4、表单特殊情况处理
当iframe标签本身id、name属性值是一串动态变化的字符串或者iframe本身没有可用的name属性时,可以借助层级定位间接地定位iframe标签。
1、通过xpath层级定位到iframe标签,将定位对象赋值给xpath;
2、通过switch_to.frame()方法定位到iframe标签;
1、GET方法实战
driver.get("https://www.baidu.com/")
driver.find_element(By.ID,"kw").send_keys("渗透吧")
driver.find_element(By.ID,"su").click()
time.sleep(1)
# 第一个窗口下点击“渗透吧”链接
driver.find_element(By.XPATH,"//*[@id='1']/h3/a").click()
# 使用get()方法获取跳转后的URL地址
driver.get("https://tieba.baidu.com/f?kw=%C9%F8%CD%B8&fr=ala0&tpl=5")
time.sleep(1)
driver.find_element(By.LINK_TEXT,"进入贴吧").click()
2、SWITCH方法实战
driver.get("https://www.baidu.com/")
driver.find_element(By.LINK_TEXT,"hao123").click()
time.sleep(1)
# 获取所有窗口的句柄
windows = driver.window_handles
driver.switch_to.window(windows[1])
time.sleep(0.5)
driver.find_element(By.XPATH,"//*[@id='search']/form/div[2]/input").send_keys("TESTING")
time.sleep(0.5)
driver.switch_to.window(windows[0])
driver.find_element(By.ID,"kw").send_keys("BAIDU")
1、获取警告框对应的文本信息:driver.switch_to.alert.text
driver.get("https://www.baidu.com/")
setting = driver.find_element(By.ID,"s-usersetting-top")
ActionChains(driver).move_to_element(setting).perform()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='s-user-setting-menu']/div/a[1]").click()
time.sleep(1)
driver.find_element(By.ID,"SL_1").click()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='se-setting-7']/a[2]").click()
time.sleep(1)
alert_text = driver.switch_to.alert.text
print(alert_text)
2、接收警告框:driver.switch_to.alert.accept() 相当于警告框中的“确认”按钮
driver.get("https://www.baidu.com/")
setting = driver.find_element(By.ID,"s-usersetting-top")
ActionChains(driver).move_to_element(setting).perform()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='s-user-setting-menu']/div/a[1]").click()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='se-setting-7']/a[2]").click()
time.sleep(1)
alert_text = driver.switch_to.alert.accept()
3、取消警告框:driver.switch_to.alert.dismiss() 相当于警告框中的“取消”按钮
driver.get("https://www.baidu.com/")
setting = driver.find_element(By.ID,"s-usersetting-top")
ActionChains(driver).move_to_element(setting).perform()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='s-user-setting-menu']/div/a[1]").click()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='se-setting-7']/a[2]").click()
time.sleep(1)
alert_text = driver.switch_to.alert.dismiss()
1、处理富文本实战
暂无富文本案例要求,后续补充;
2、处理隐藏元素实战
元素被隐藏:style= "display:none;"
设置元素可见:style.diaplay = "block"
3、处理readonly属性实战
移除该属性:readonly = "readonly"
4、处理浏览器滚动条操作
1、execute_script()
window.scrollTo(0,10000):以像素为单位;
2、引用键盘Keys类:使用Keys类键盘实现滚动条,只需要指明滚动条的目标位置;
driver.get("https://www.baidu.com/")
driver.find_element(By.ID,"kw").send_keys("selenium")
driver.find_element(By.ID,"su").click()
time.sleep(1)
driver.execute_script("window.scrollTo(0,10000);")
time.sleep(2)
driver.execute_script("window.scrollTo(10000,0);")
time.sleep(2)
from selenium.webdriver.common.keys import Keys
driver.get("https://www.baidu.com/")
driver.find_element(By.ID,"kw").send_keys("selenium")
driver.find_element(By.ID,"su").click()
time.sleep(1)
driver.find_element(By.XPATH,"//*[@id='page']/div/a[10]").send_keys(Keys.DOWN)
time.sleep(2)
driver.find_element(By.XPATH,"//*[@id='s_tab']/div/a[9]").send_keys(Keys.UP)
time.sleep(2)