url = 'https://chat18.aichatos8.com'
chrome_binary_path = '/Users/baidu/project/script/chromedriver/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing'
chromedriver_path = '/Users/baidu/project/script/chromedriver/chromedriver-mac-arm64/chromedriver'
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.binary_location = chrome_binary_path
options.add_argument('--enable-logging')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# options.add_argument('window-size=1920x3000') #指定瀏覽器解析度
options.add_argument('--disable-gpu') #谷歌文件提到需要加上這個屬性來規避bug
options.add_argument('--hide-scrollbars') #隱藏捲軸, 應對一些特殊頁面
# options.add_argument('blink-settings=imagesEnabled=false') #不載入圖片, 提升速度
# options.add_argument('--headless') #瀏覽器不提供視覺化頁面
driver = webdriver.Chrome(options=options)
driver.service.path = chromedriver_path
# driver.get(url)
# driver.implicitly_wait(10)
# performance_log = driver.execute_script("return window.performance.getEntries();")
# for entry in performance_log:
# print(entry)
# # 獲取效能日誌
# # logs = driver.get_log('client')
# # for entry in logs:
# # message = entry['message']
# # if '"method":' in message:
# # request = message.split('"params":')[1]
# # print(request)
# # 退出WebDriver例項
# driver.quit()
# -*- coding: utf-8 -*-
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
# driver = Chrome('C:\Program Files\Google\Chrome\Application\chromedriver.exe')
#
# driver.get('https://www.66law.cn/')
# search = driver.findElement(By.CLASS_NAME,"stairway")
# _webdriver = driver.find_element(By.ID, "value")
# _webdriver = driver.find_element(By.CLASS_NAME, "value")
# _webdriver = driver.find_element(By.NAME, "value")
# _webdriver = driver.find_element(By.TAG_NAME, "value")
# _webdriver = driver.find_element(By.XPATH, "value")
# # _webdriver = driver.find_element_by_css_selector("value") # 利用CSS選擇器查詢
# _webdriver = driver.find_element(By.CSS_SELETOR, "value")
# _webdriver = driver.find_element(By.ID, "su").click()
# _webdriver.send_keys('電腦')
# _webdriver.send_keys(Keys.key)
# 重新整理, 後退, 前進
# _webdriver.refresh()
# _webdriver.back()
# _webdriver.forward()
# # 切換選項卡
# driver.switch_to.window(driver.window_handles[0])
# time.sleep(2)
# driver.switch_to.window(driver.window_handles[1])
# driver.get(url)
# driver.quit()
# 登入163郵箱
# driver.get('https://mail.163.com/')
# frame = driver.find_element(By.CSS_SELECTOR, "#loginDiv>iframe")
# driver.switch_to.frame(frame)
# user_name = driver.find_element(By.CSS_SELECTOR,'.j-inputtext.dlemail.j-nameforslide')
# user_name.send_keys('asuphy')
# password = driver.find_element(By.CSS_SELECTOR,'.j-inputtext.dlpwd')
# password.send_keys('woxihuanni0')
# enter = driver.find_element(By.CSS_SELECTOR,'#dologin')
# enter.click()
# driver.switch_to.frame("輸入網頁的id或者name")
# # /從子frame切回到父frame
# driver.switch_to.parent_frame()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
def get_tracks(distance):
"""
拿到移動軌跡,模仿人的滑動行為,先勻加速後勻減速
勻變速運動基本公式:
①v = v0+at
②s = v0t+1/2at^2
"""
#初速度
v = 0
#單位時間為0.3s來統計軌跡,軌跡即0.3內的位移
t = 0.31
#位置/軌跡列表,列表內的一個元素代表0.3s的位移
tracks = []
#當前位移
current = 0
#到達mid值開始減速
mid = distance*4/5
while current < distance:
if current < mid:
#加速度越小,單位時間內的位移越小,模擬的軌跡就越多越詳細
a = 2
else:
a = -3
#初速度
v0 = v
#0.3秒內的位移
s = v0*t+0.5*a*(t**2)
#當前的位置
current += s
#新增到軌跡列表
tracks.append(round(s))
#速度已經到達v,該速度作為下次的初速度
v = v0+a*t
return tracks
driver.get("http://www.douban.com/")
#切換到登陸框架中來
driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])
driver.implicitly_wait(2)
driver.find_element(By.CLASS_NAME, "account-tab-account").click()
# 輸入賬號密碼
input1 = driver.find_element(By.ID, "username")
input1.clear() #清空輸入框預設內容
input1.send_keys("賬號")
input2 = driver.find_element(By.ID, "password")
input2.clear()
input2.send_keys("密碼")
#登陸點選
driver.find_element(By.CLASS_NAME, "btn-account").click()
driver.implicitly_wait(5)
#如果定位節點在標籤iframe內,那麼則必須使用switch_to進行iframe的切換
driver.switch_to.frame(driver.find_element(By.ID, "tcaptcha_iframe"))
#按住開始滑動位置按鈕-先移動180個畫素
element = driver.find_elements(By.XPATH, '//div[@id="tcaptcha_drag_thumb"]')[0]
driver.implicitly_wait(2)
ActionChains(driver).move_to_element(element).click_and_hold(element).perform()
# move_to_element_with_offset()移動到距離某個元素左上角多少距離的位置
ActionChains(driver).move_to_element_with_offset(to_element=element,xoffset=180,yoffset=0).perform()
#使用加速度函式移動剩下的距離
tracks = get_tracks(26)
for track in tracks: #開始移動move_by_offset()
ActionChains(driver).move_by_offset(xoffset=track,yoffset=0).perform()
#延遲釋放滑鼠:release()
driver.implicitly_wait(2)
ActionChains(driver).release().perform()
driver.implicitly_wait(10)
# driver.get_cookies()
# 生成登陸後快照
# driver.save_screenshot("douban.png")
# # 儲存原始碼
# with open("douban.html", "w", encoding='utf-8') as file:
# file.write(driver.page_source)
driver.quit()