pip install selenium
css
'--proxy-server=http://IP:Port'
)import time from selenium import webdriver driver = webdriver.Chrome() try: driver.get("http://news.baidu.com/") print('執行js打開新窗口前') print('全部窗口的句柄:', driver.window_handles) print('當前窗口的句柄:', driver.current_window_handle) # 執行js打開新窗口 new_js = 'window.open("https://www.toutiao.com/")' driver.execute_script(new_js) print('執行js打開新窗口後') all_handle = driver.window_handles print('全部窗口的句柄:', all_handle) print('當前窗口的句柄:', driver.current_window_handle) print('切換句柄...') driver.switch_to.window(all_handle[1]) print('當前窗口的句柄:', driver.current_window_handle) # 頁面高度 height = 0 # 執行js滑動滾動條 while height < 10000: # 滑動滾動條到指定位置 driver.execute_script('document.documentElement.scrollTop=10000') # 頁面高度 height = driver.execute_script('return document.body.scrollHeight') time.sleep(2) # 關閉新打開的窗口 driver.close() finally: time.sleep(3) # 關閉瀏覽器 driver.quit()
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() try: driver.get("https://qzone.qq.com/") time.sleep(0.5) """經過元素定位,切到指定frame(登陸框) frame = driver.find_element(By.XPATH,'//iframe') driver.switch_to.frame(frame) """ # 也可經過 ID 或 name 定位 frame driver.switch_to.frame('login_frame') # 點擊 賬號密碼登陸 driver.find_element(By.LINK_TEXT, '賬號密碼登陸').click() time.sleep(0.5) text_box = driver.find_element(By.ID, 'u') # 輸入內容 text_box.send_keys('0123456789') time.sleep(1) # 清除內容 text_box.clear() time.sleep(1) text_box.send_keys('正確的帳號') password_box = driver.find_element(By.CSS_SELECTOR, '.inputstyle.password') password_box.send_keys('正確的密碼') time.sleep(1.5) # 執行鍵盤操做:回車鍵 password_box.send_keys(Keys.ENTER) time.sleep(7) finally: time.sleep(3) # 關閉瀏覽器 driver.quit()
import time from selenium import webdriver driver = webdriver.Chrome() try: driver.get("https://www.baidu.com/") time.sleep(0.3) driver.find_element_by_link_text('設置').click() time.sleep(0.3) driver.find_element_by_link_text('搜索設置').click() time.sleep(0.3) driver.find_element_by_link_text('保存設置').click() time.sleep(1) # 獲取彈窗的對象 alert_box = driver.switch_to.alert # 獲取彈窗的內容 print('彈窗內容:',alert_box.text) # 對彈窗對象的肯定操做 alert_box.accept() # 按鈕:百度一下 button = driver.find_element_by_css_selector('[type="submit"]') # 獲取元素標籤上的屬性 print('get_attribute:', button.get_attribute('value')) # 元素的座標 print('location:', button.location) # 元素的大小 print('size:', button.size) # 截圖 driver.get_screenshot_as_file('1.png') finally: time.sleep(3) # 關閉瀏覽器 driver.quit()
import time from selenium import webdriver driver = webdriver.Chrome() try: driver.get("https://www.toutiao.com/") time.sleep(1) # 最小化 driver.minimize_window() time.sleep(1) # 設置窗口具體的大小 driver.set_window_size(500,500) time.sleep(1) # 設置窗口位置 driver.set_window_position(800,200) time.sleep(1) # 最大化 driver.maximize_window() time.sleep(1) # 打印網頁源碼(JS渲染過的) print(driver.page_source) # 刷新當前頁面 driver.refresh() finally: time.sleep(3) # 關閉瀏覽器 driver.quit()
import time import json from selenium import webdriver driver = webdriver.Chrome() try: driver.get('https://www.cnblogs.com/') """獲取Cookie # 等待60秒用於手動登陸 time.sleep(60) # 保存獲取到的Cookie cookies = driver.get_cookies() with open('1.txt', 'w+') as f: f.write(json.dumps(cookies)) """ """添加Cookie with open('1.txt') as f: cookies = json.loads(f.read()) # 添加cookie前須要先打開任意一個網頁 # 而後再進行添加 # 不然會報錯:InvalidCookieDomainException: Message: invalid cookie domain for cookie in cookies: driver.add_cookie(cookie) # 添加完畢後,再打開對應網頁,即是已登陸狀態 driver.get('https://www.cnblogs.com/') time.sleep(5) """ """清空cookie driver.delete_all_cookies() # 清空cookie後是未登陸的狀態 driver.get('https://www.cnblogs.com/') """ finally: time.sleep(3) driver.quit()
import time from selenium import webdriver opt = webdriver.ChromeOptions() # 設置代理 opt.add_argument('--proxy-server=http://121.40.162.239:808') # 設置UA opt.add_argument('User-Agent=ABCDEFG') driver = webdriver.Chrome(options=opt) try: driver.get('http://httpbin.org/get') finally: time.sleep(3) driver.quit()
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() try: driver.get('https://www.python.org/getit/') # 顯式等待的等待條件 --> 參數爲數組類型 method = EC.presence_of_element_located((By.LINK_TEXT, 'Downloads')) # 顯式等待 WebDriverWait(driver, 20, 0.2).until(method) # 隱式等待 # driver.implicitly_wait(20) driver.find_element(By.LINK_TEXT, 'Downloads').click() finally: time.sleep(10) driver.quit()
import time from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() try: driver.get('http://www.treejs.cn/v3/demo/cn/exedit/drag.html') time.sleep(2) # 定位元素 element = driver.find_element_by_id('treeDemo_2_span') target = driver.find_element_by_id('treeDemo_12_span') # 動做鏈:拖拽 ActionChains(driver).drag_and_drop(element,target).perform() finally: time.sleep(10) driver.quit()
import time from selenium import webdriver opt = webdriver.ChromeOptions() opt.add_experimental_option('excludeSwitches', ['enable-automation']) driver = webdriver.Chrome(options=opt) try: driver.get('http://www.baidu.com') finally: time.sleep(10) driver.quit()