Selenium API
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
driver = webdriver.Chrome()
driver.maximize_window()
driver.set_window_size(800,600)
size = driver.get_window_size()
driver.set_window_position(0,0)
position = driver.get_window_position()
driver.close()
driver.quit()
driver.get( "https://www.baidu.com")
driver.refresh()
driver.back()
driver.forward()
print(driver.current_url)
print(driver.title)
driver.get_screenshot_as_file("screeshot01.png")
date = driver.get_screenshot_as_png()
with open("screenshot02.png","wb") as f:
f.write(date)
date64 = driver.get_screenshot_as_base64()
print(type(date64))
with open("screenshot03.png","w") as f:
f.write(date64)
sourse = driver.page_source
with open("CSDN.html","wb") as f:
f.write(sourse.encode())
el = driver.find_element_by_id("toolbar-search-input")
# el = driver.find_element(By.ID,"toolbar-search-input")
el = driver.find_element_by_name("pwd")
el = driver.find_element_by_class_name("user-service-link")
el = driver.find_element_by_tag_name("input")
el = driver.find_element_by_link_text("首页")
el = driver.find_element_by_partial_link_text("首")
el = driver.find_element_by_css_selector("#app ul > li:nth-child(2) > a")
el = driver.find_element_by_xpath('//*[@id="tabTwo"]')
el = driver.find_elements_by_xpath('//*[@id="floor-sp_66"]/h4')
# element.click()
# element.clear()
# element.send_keys()
# element.submit()
# element.text
# element.get_attribute("attrName")
# element.size
# element.is_displayed()
handles = driver.window_handles
driver.switch_to.window(handles[0])
driver.switch_to.frame(el_frame) # 方式一:先定位,后切换
# driver.switch_to.frame("id") # 方式二:也可以直接使用id进行切换(如果有固定id的话)
driver.switch_to.default_content()
driver.switch_to.parent_frame()
driver.switch_to.alert()
driver.switch_to.alert.accept()
driver.switch_to.alert.dismiss()
# driver.switch_to.alert.send_keys("data")
from selenium.webdriver import ActionChains
# 鼠标右击操作,操作前需要将元素定位出来并传入相应的动作中;如果要执行操作,需要调用perform()
ActionChains(driver).context_click(el_logo).perform()
# 鼠标双击操作
ActionChains(driver).double_click(el_logo).perform()
ActionChains(driver).move_to_element(el_float).perform()
from selenium.webdriver.common.keys import Keys
el_search.send_keys(Keys.CONTROL,"a")
el_search.send_keys(Keys.CONTROL,"x")
el_search.send_keys("hello "+Keys.CONTROL,"v")
el_search.send_keys(Keys.BACKSPACE) # BACK_SPACE = BACKSPACE
from selenium.webdriver.support.select import Select
selObj = Select(ele_select)
selObj.select_by_index("0") # 通过索引选择,str
selObj.select_by_value("10") # 通过value值选择
selObj.select_by_visible_text("50") # 通过文本选择
all = selObj.all_selected_options # 查看所有已选
first = selObj.first_selected_option # 查看第一个已选
isMu = selObj.is_multiple # 查看是否多选
opt = selObj.options # 查看选项元素列表
print(all,first,isMu,opt)
调用JS代码
driver.get(
# 页面滚动
js1 = "window.scrollTo(0,2000)"
js2 = "var q = document.documentElement.scrollTop = 500"
driver.execute_script(js1)
driver.execute_script(js2)
# 循环下拉滚动
for i in range(20):
js = "window.scrollTo(0,%s)" %(i*100)
driver.execute_script(js)
time.sleep(0.5)
WebDriverWait(driver,timeout=10,poll_frequency=0.5,ignored_exceptions=None).until(EC.presence_of_element_located((By.ID,"toolbar-search-input"))) # 在设置的时间内,默认每隔一段时间检测一次当前页面元素是否存在,如果超过时间检测不到则抛出异常
# 隐式等待
driver.implicitly_wait(10) # 单位为秒,为最大值;在这个最大值内只要定位到就结束等待,否则报错
cookies操作
# 获取cookies --> 字典dict格式
cookies = driver.get_cookies()
# 获取key对应的值
# driver.get_cookie(keyName)
# 删除指定名称的cookie
# driver.delete_cookie(name="xx")
# 删除所有cookies
driver.delete_all_cookies()
# 设置cookies
cookie = {'domain': '.csdn.net', 'expiry': 1645971310, 'httpOnly': False, 'name': 'Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac', 'path': '/', 'secure': False, 'value': '6525*1*10_18797500640-1614435306890-873769'}
driver.add_cookie(cookie)