selenium 初试
from selenium import webdriver
from time import sleep
from lxml import etree
bro = webdriver.Chrome()
bro.get('https://www.taobao.com/')
page_text = bro.page_source
tree = etree.HTML(page_text)
bro.quit()
selenium 例子
from selenium import webdriver
import time
bro = webdriver.Chrome()
bro.get('https://www.taobao.com/')
bro.find_element_by_id('q').send_keys('python')
bro.execute_script('window.scrollTo(0,document.body.scrollHeight')
btn = bro.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/div[1]/div[2]/form/div[1]/button')
btn.click()
time.sleep(2)
bro.quit()
selenium 动作链
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
bro = webdriver.Chrome()
bro.get('url')
bro.switch_to.frame('iframeResult')
div = bro.find_element_by_id('draggable')
action = ActionChains()
action.click_and_hold(div)
for i in range(5):
action.move_by_offset(17,0).perform()
action.release()
bro.quit()
selenium iframe
from selenium import webdriver
from time import sleep
from lxml import etree
from selenium.webdriver import ActionChains
bro = webdriver.Chrome()
bro.get('https://www.taobao.com/')
bro.switch_to.frame('iframeResult')
div = bro.find_element_by_id('draggable')
action = ActionChains(bro)
action.click_and_hold(div)
for i in range(5):
action.move_by_offset(17,0).perform()
sleep(0.3)
action.release()
bro.quit()
selenium 无可视化界面 规避检测
from selenium import webdriver
from time import sleep
#实现无可视化界面的
from selenium.webdriver.chrome.options import Options
#实现规避检测的
from selenium.webdriver import ChromeOptions
#无可视化界面
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
#规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
#新建一个类
bro = webdriver.Chrome(chrome_options=chrome_options,options=option)
#发起请求
bro.get('https://www.taobao.com/')
print(bro.page_source)
sleep(3)
bro.quit()
|