2022/05/16学习内容 整理时间:2022/05/17 参考资料:https://www.bilibili.com/video/BV1NM4y1K73T?p=1&spm_id_from=333.851.header_right.history_list.click
selenium实例
7.浏览器的其它操作 manpi_browser.py 1)最大化 2)后退/前进/刷新 3)关闭当前页面 4)关闭浏览器
8.浏览器属性获取 manpi_browser.py 1)获取当前页面title 2)获取当前页面URL
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("chromedriver.exe")
url = "https://www.baidu.com/"
driver.get(url)
driver.maximize_window()
print(driver.title)
print(driver.current_url)
driver.find_element_by_xpath("//*[@id='kw']").send_keys("python")
driver.find_element_by_xpath("//*[@id='su']").click()
time.sleep(3)
print(driver.title)
print(driver.current_url)
driver.back()
time.sleep(3)
print(driver.title)
print(driver.current_url)
driver.quit()
9.页面等待 wait_element.py 1)强制 2)显式 3)隐式
from selenium import webdriver
import time
driver = webdriver.Chrome("chromedriver.exe")
url = "https://www.baidu.com/"
driver.get(url)
driver.find_element_by_xpath("//*[@id='kw']").send_keys("python")
driver.find_element_by_xpath("//*[@id='su']").click()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//*[@id='1']").click()
time.sleep(3)
driver.quit()
10.模拟鼠标操作 manpi_mouse.py 步骤: 1)创建ActionChains对象 2)点击操作:右击、悬停、双击、拖动等 3)perform()执行操作
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe")
url = "https://www.baidu.com/"
driver.get(url)
time.sleep(3)
chains = ActionChains(driver)
time.sleep(3)
driver.quit()
11.模拟键盘操作 manpi_keyboard.py
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe")
url = "https://www.baidu.com/"
driver.get(url)
el = driver.find_element_by_xpath("//*[@id='kw']")
el.send_keys("python")
time.sleep(3)
el.send_keys(Keys.CONTROL, "a")
time.sleep(3)
el.send_keys(Keys.BACKSPACE)
time.sleep(3)
el.send_keys("java")
time.sleep(3)
el.send_keys(Keys.CONTROL, "a")
time.sleep(3)
el.send_keys(Keys.CONTROL, "c")
time.sleep(3)
el.send_keys(Keys.CONTROL, "v")
time.sleep(3)
driver.quit()
from selenium.webdriver.support.select import Select
select = Select(driver.find_element_by_xpath("//*[@id='selectA']"))
time.sleep(3)
select.select_by_index(3)
time.sleep(3)
select.select_by_value("bj")
time.sleep(3)
select.select_by_visible_text("A广东")
time.sleep(3)
12.下拉框处理 select.py 1)概念:下拉框是select元素。其中的每个选项是option 2)选中下拉框选项方式:索引、值、文本
import time
from selenium.webdriver.support.select import Select
浏览器其他操作/属性获取+页面等待+模拟鼠标 模拟键盘+下拉款
|