pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# 1.访问CSDN
driver = webdriver.Chrome()
driver.get('https://www.csdn.net/') # 访问CSDN
# 2.根据标签ID搜索内容并点击回车
driver.find_element(By.ID, 'toolbar-search-input').send_keys('python', Keys.ENTER) # 搜索python关键字并回车
# 设置浏览器浏览器的宽高为:600x800
driver.set_window_size(600, 800)
# 全屏展示浏览器
driver.maximize_window()
from selenium import webdriver
import time
driver = webdriver.Chrome()
# 访问CSDN首页
driver.get('https://www.csdn.net/')
time.sleep(2) #等待2秒
#访问CSDN个人主页
driver.get('https://blog.csdn.net/qq_40216188')
time.sleep(2)
#返回(后退)到CSDN首页
driver.back()
time.sleep(2)
#前进到个人主页
driver.forward()
# 在原页面打开
driver.get('https://blog.csdn.net/qq_40216188')
# 新中打开
js = "window.open('https://blog.csdn.net/qq_40216188')"
driver.execute_script(js)
# 刷新页面
driver.refresh()
driver.quit()
driver.close()
switch_to.windows()
方法。from selenium import webdriver
handles = []
driver = webdriver.Chrome()
driver.get('https://www.csdn.net')
driver.maximize_window()
# 设置隐式等待
driver.implicitly_wait(3)
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
# 点击进入新页面
driver.find_element(By.XPATH,
'//*[@id="www-home-right"]/div[1]/div[1]/div[4]/div/span').click()
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
print(handles)
# 获取当前所有窗口的句柄
print(driver.window_handles)
switch_to.windows()
进行切换from selenium import webdriver
handles = []
driver = webdriver.Chrome()
driver.get('https://www.csdn.net')
driver.maximize_window()
# 设置隐式等待
driver.implicitly_wait(3)
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
# 点击页面
driver.find_element(By.XPATH,
'//*[@id="www-home-right"]/div[1]/div[1]/div[4]/div/span').click()
# 切换到当前最新打开的窗口
driver.switch_to.window(driver.window_handles[-1])
# 获取当前窗口的句柄
handles.append(driver.current_window_handle)
print(handles)
# 获取当前所有窗口的句柄
print(driver.window_handles)
handles = driver.window_handles #获取当前浏览器的所有窗口句柄
driver.switch_to.window(handles[-1]) #切换到最新打开的窗口
driver.switch_to.window(handles[-2]) #切换到倒数第二个打开的窗口
driver.switch_to.window(handles[0]) #切换到最开始打开的窗口
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.csdn.net/')
time.sleep(2)
# 定位搜索输入框
text_label = driver.find_element(By.XPATH, '//*[@id="toolbar-search-input"]')
# 模拟输入:python
text_label.send_keys('python')
time.sleep(2)
# 清除搜索框中的内容
text_label.clear()
# 输出搜索框元素是否可见
print(text_label.is_displayed())
# 输出placeholder属性值
print(text_label.get_attribute('placeholder'))
# 定位搜索按钮
button = driver.find_element(By.XPATH, '//*[@id="toolbar-search-button"]/span')
# 输出按钮的大小
print(button.size)
# 输出按钮上的文本
print(button.text)
'''输出内容
True
python面试100问
{'height': 32, 'width': 28}
搜索
'''
# 定位点击内容
button=driver.find_element(By.XPATH, '//*[@id="toolbar-search-button"]/span')
# 点击
button.click()
.perform()函数:执行所有ActionChains中存储的动作
from selenium.webdriver.common.action_chains import ActionChains
# 定位搜索按钮
button = driver.find_element(By.XPATH, '//*[@id="toolbar-search-button"]/span')
# 右键搜索按钮
ActionChains(driver).context_click(button).perform()
# 定位搜索按钮
button = driver.find_element(By.XPATH, '//*[@id="toolbar-search-button"]/span')
# 执行双击动作
ActionChains(driver).double_click(button).perform()
# 定位要拖动的元素
source = driver.find_element(By.XPATH,'xxx')
# 定位目标元素
target = driver.find_element(By.XPATH,'xxx')
# 执行拖动动作
ActionChains(driver).drag_and_drop(source, target).perform()
# 定位收藏栏
collect = driver.find_element(By.XPATH, '//*[@id="csdn-toolbar"]/div/div/div[3]/div/div[3]/a')
# 悬停至收藏标签处
ActionChains(driver).move_to_element(collect).perform()
from selenium.webdriver.common.keys import Keys
# 定位输入框并输入文本
driver.find_element(By.ID, 'xxx').send_keys('python')
# 模拟回车键进行跳转(输入内容后)
driver.find_element(By.ID, 'xxx').send_keys(Keys.ENTER)
# 使用 Backspace 来删除一个字符
driver.find_element(By.ID,'xxx').send_keys(Keys.BACK_SPACE)
# Ctrl + A 全选输入框中内容
driver.find_element(By.ID,'xxx').send_keys(Keys.CONTROL, 'a')
# Ctrl + C 复制输入框中内容
driver.find_element(By.ID,'xxx').send_keys(Keys.CONTROL, 'c')
# Ctrl + V 粘贴输入框中内容
driver.find_element(By.ID,'xxx').send_keys(Keys.CONTROL, 'v')
# F1键
driver.find_element(By.ID,'xxx').send_keys(Keys.F1)
# 空格
driver.find_element(By.ID,'xxx').send_keys(Keys.SPACE)
# Tab键
driver.find_element(By.ID,'xxx').send_keys(Keys.TAB)
# ESC键
driver.find_element(By.ID,'xxx').send_keys(Keys.ESCAPE)
# Alt键
driver.find_element(By.ID,'xxx').send_keys(Keys.ALT)
# Shift键
driver.find_element(By.ID,'xxx').send_keys(Keys.SHIFT)
# 向下箭头
driver.find_element(By.ID,'xxx').send_keys(Keys.ARROW_DOWN)
# 向左箭头
driver.find_element(By.ID,'xxx').send_keys(Keys.ARROW_LEFT)
# 向右箭头
driver.find_element(By.ID,'xxx').send_keys(Keys.ARROW_RIGHT)
# 向上箭头
driver.find_element(By.ID,'xxx').send_keys(Keys.ARROW_UP)
WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
"""
driver:浏览器驱动
timeout:超时时间,单位秒
poll_frequency:每次检测的间隔时间,默认为0.5秒
ignored_exceptions:指定忽略的异常,如果在调用 until 或 until_not 的过程中抛出指定忽略的异常,则不中断代码,默认忽略的只有 NoSuchElementException
"""
until(method, message='')
until_not(method, message='')
"""
method:指定预期条件的判断方法,在等待期间,每隔一段时间调用该方法,判断元素是否存在,直到元素出现。until_not 正好相反,当元素消失或指定条件不成立,则继续执行后续代码;
message: 如果超时,抛出 TimeoutException ,并显示 message 中的内容
"""
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
element = WebDriverWait(driver, 5, 0.5).until(
EC.presence_of_element_located((By.ID, 'kw')),
message='超时啦!')
implicitly_wait()
来实现隐式等待,使用难度相对于显式等待要简单很多。from selenium import webdriver
from time import time
driver = webdriver.Chrome()
driver.get('https://blog.csdn.net/qq_40216188')
start = time()
driver.implicitly_wait(5) #轮训5秒之后仍没有找到定位元素,抛出异常
try:
driver.find_element(By.ID,'kw')
except Exception as e:
print(e)
print(f'耗时:{time()-start}')
"""结果
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="kw"]"}
(Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
Ordinal0 [0x00ED1ED3+2236115]
Ordinal0 [0x00E692F1+1807089]
Ordinal0 [0x00D766FD+812797]
Ordinal0 [0x00DA55DF+1005023]
Ordinal0 [0x00DA57CB+1005515]
Ordinal0 [0x00DD7632+1209906]
Ordinal0 [0x00DC1AD4+1120980]
Ordinal0 [0x00DD59E2+1202658]
Ordinal0 [0x00DC18A6+1120422]
Ordinal0 [0x00D9A73D+960317]
Ordinal0 [0x00D9B71F+964383]
GetHandleVerifier [0x0117E7E2+2743074]
GetHandleVerifier [0x011708D4+2685972]
GetHandleVerifier [0x00F62BAA+532202]
GetHandleVerifier [0x00F61990+527568]
Ordinal0 [0x00E7080C+1837068]
Ordinal0 [0x00E74CD8+1854680]
Ordinal0 [0x00E74DC5+1854917]
Ordinal0 [0x00E7ED64+1895780]
BaseThreadInitThunk [0x7534FA29+25]
RtlGetAppContainerNamedObjectPath [0x774D7BBE+286]
RtlGetAppContainerNamedObjectPath [0x774D7B8E+238]
耗时:5.058804988861084
"""
from selenium import webdriver
from time import time, sleep
driver = webdriver.Chrome()
driver.get('https://blog.csdn.net/qq_40216188')
start = time()
sleep(5)
try:
driver.find_element(By.ID, 'kw')
except Exception as e:
print(e)
print(f'耗时:{time()-start}')
driver.find_element(By.ID, "toolbar-search-input")
driver.find_element(By.NAME, "keywords")