下载驱动:
链接:https://pan.baidu.com/s/14veD_nqKJgZJG6Op0X-upQ
提取码:p3ig
设置环境变量:
将火狐和chrome的两个驱动所在的文件夹添加到电脑的环境变量中去,文件路径不能有中文
安装selenium库
pip install selenium |
chrome 的代码1
注意:当python连接chrome的时候,不要手动打开chrome浏览器 会报错:unknown error: DevToolsActivePort file doesn't exist,The process started from chrome location..... # coding = utf-8 from selenium import webdriver # 个人资料路径 user_data_dir = r'--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data' # 加载配置数据 option = webdriver.ChromeOptions() option.add_argument(user_data_dir) # 启动浏览器配置 driver = webdriver.Chrome(chrome_options=option, executable_path=r'D:\coship\Test_Framework\drivers\chromedriver.exe') driver.get(r'https://www.cnblogs.com/') |
chrome 的代码2
注意:当python连接chrome的时候,不要手动打开chrome浏览器
from selenium import webdriver chrome_options = webdriver.ChromeOptions() #无头点击下载文件,但是有时候失效 # prefs = {'download.default_directory': p, # 'download.prompt_for_download': False, # 'download.directory_upgrade': True, # 'safebrowsing.enabled': False, # 'safebrowsing.disable_download_protection': True} downpath="D:\\down" chrome_options.add_experimental_option("prefs", {"download.default_directory": downpath, "download.prompt_for_download": False, }) # chrome_options.add_argument("--headless") #无头设置 # chrome_options.add_argument('--window-size=1920,1080') driver = Chrome(chrome_options=chrome_options) driver.implicitly_wait(10) url = "https://www.baidu.com/" driver.get(url) |
firefox:
from selenium.webdriver.firefox.options import Options from selenium.webdriver import Firefox from selenium import webdriver options = Options() options.add_argument('--window-size=1920,1080') #options.add_argument('-headless') # 加载无头浏览配置 profile_path = r'C:\Users\L\AppData\Roaming\Mozilla\Firefox\Profiles\wgc3wo5q.default' profile = webdriver.FirefoxProfile(profile_path ) # 指定配置文件地址 profile.set_preference('browser.download.folderList', 2) profile.set_preference('browser.download.manager.showWhenStarting', False) #下载不弹窗 downpath="D:\\down" profile.set_preference('browser.download.dir', downpath) # 指定下载位置 driver = webdriver.Firefox(profile, options=options) # 加载配置,构建driver driver.implicitly_wait(10) url = "https://www.baidu.com/" driver.get(url) |
验证码登录
伪代码:
jt = 'D:\\huohu\\截图' #存放截图的文件夹 try: os.makedirs(jt) except: pass sleep(1) driver.find_element_by_id("password").clear() sleep(1) driver.find_element_by_id("password").send_keys(value) #输入密码 driver.save_screenshot(jt+'\\sg_save_jietu.png') #截屏 imgelement = driver.find_element_by_id('validateCodeImg') location = imgelement.location # 获取验证码x,y轴坐标 size = imgelement.size # 获取验证码的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open(jt+'\\sg_save_jietu.png') frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 yzm_path = (jt+'\\sg_yzm_path.png') frame4.save(yzm_path) sleep(1) rc = RClient('user', 'pwd', '119287', 'e96fec5c66884adf8361579ecce0903f') #接入若快打码 sleep(1) im = open(yzm_path, 'rb').read() sleep(1.5) yzm=rc.rk_create(im, 3040)["Result"] sleep(0.5) driver.find_element_by_id("validateCode").clear() #清空验证码 sleep(1) driver.find_element_by_id('validateCode').send_keys(yzm) #输入验证码 sleep(1.5) driver.find_element_by_id("button").click() #确定登录 |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理