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()