欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

selenium Python

程序员文章站 2022-07-14 09:52:24
...

很久之前就听说过selenium,也用过当年的selenium1 remote control

今天准备尝试从gitlab上下载文件到桌面,因为gitlabAPI授权问题,于是果断选用selenium了

用的是selenium-3.11.0版本的,有些2点几的版本缺少一些API。用的是chrome,外加一个放在桌面的chrome驱动器

然后代码如下:

# coding:utf-8
from selenium import webdriver
import time
from dir_operation import cp_zip

"""
以下这块关于下载到指定位置无效
"""
# options = webdriver.ChromeOptions()
# options.add_argument('--ignore-certificate-errors')
# prefs = {'profile.default_content_settings.popus': 0,
#          'download.default_directory': r'D:\Users\EX-FEIXIAOHUI001\Downloads\demaxiya',
#          'download.prompt_for_download': False,
#          'download.directory_upgrade': True,
#          'safebrowsing.enabled': True
#          }
# options.add_experimental_option('prefs', prefs)

# driver = webdriver.Chrome("D:/Users/EX-FEIXIAOHUI001/Downloads/chromedriver.exe",chrome_options=options)

driver = webdriver.Chrome("D:/Users/EX-FEIXIAOHUI001/Downloads/chromedriver.exe")
driver.get("http://pagit.paic.com.cn/users/sign_in?redirect_to_referer=yes")
driver.find_element_by_id("username").send_keys("EX-FEIXIAOHUI001")
driver.find_element_by_id("password").send_keys("*****")
driver.find_element_by_css_selector(".btn.btn-save").click()
driver.get("http://pagit.paic.com.cn/EX-FEIXIAOHUI001/oozie/repository/archive.zip")
driver.get("http://pagit.paic.com.cn/EX-FEIXIAOHUI001/project_config/repository/archive.zip")
time.sleep(2)
driver.quit()

try:
	cp_zip()
except Exception,e:
	print e


上述本应该能实现下载文件到指定位置,但是由于公司电脑权限问题,就只能下到默认目录了。

cp_zip函数的代码如下:

import os
import shutil
import glob



class auto_deploy():
	def del_dir_tree(self,path):
		if os.path.isfile(path):
			try:
				os.remove(path)
			except Exception, e:
				print e
		elif os.path.isdir(path):
			for item in os.listdir(path):
				itempath = os.path.join(path, item)
				self.del_dir_tree(itempath)
			try:
				os.rmdir(path)
			except Exception, e:
				print e
				
	def get_recent_zip(self,source_zip_path):
		files_path = os.path.join(source_zip_path, '*')
		files = sorted(glob.iglob(files_path), key=os.path.getctime, reverse=True)
		zip_one_path = files[0]
		zip_two_path = files[1]
		return zip_one_path,zip_two_path

def cp_zip():
	target_zip_path = "D:\Users\EX-FEIXIAOHUI001\Desktop\zip_project_and_config"
	instance = auto_deploy()
	if os.path.exists(target_zip_path):
		instance.del_dir_tree(target_zip_path)
	os.mkdir(target_zip_path)
	
	source_zip_path = "D:\Users\EX-FEIXIAOHUI001\Downloads"
	zip_one_path,zip_two_path = instance.get_recent_zip(source_zip_path)
	shutil.copy(zip_one_path,target_zip_path)
	shutil.copy(zip_two_path,target_zip_path)