from selenium import webdriver
webdriver创建一个浏览器
driver = webdriver.Edge()
进行操作
url = "XXX"
访问url
driver.get(url)
定位
id_pla = driver.find_element_by_id()
标签交互
id_pla.send_keys("xxx")
获取网页源码
page = driver.page_source
page.encode("utf8")
执行js程序
driver.execute_script('xxx')
前进后退
driver.back()
driver.forward()
关闭
driver.quit()
处理iframe
driver.switch_to.frame("id")
动作链
from selenium.webdriver import ActionChains
action = ActionChains(driver)
action.click_and_hold()
action.click()
action.move_by_offset(x, y)
action.perform()
action.release()
无头浏览器
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
br = webdriver.Chrome(chrome_options=chrome_options)
实现规避检测
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
br = webdriver.Chrome(chrome_options=chrome_options, options=option)
|