第二章:python-selenium自动化常用方法

python给出了一套完整的操作selenium自动化的模块和方法

编写自动化脚本通常需要使用webdriver,requests,os,time等模块

webdriver是最基础的模块,选择对应的chrome或者IE浏览器打开网页,配置打开网页的基本信息,网页元素操作都在webdriver中有特定的方法

#网页的相关配置
def option():
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_experimental_option('excludeSwitches', ['enable-automation'])
    chromeOptions.add_argument('--disable-infobars')
    chromeOptions.add_argument('--start-maximized')
    #chromeOptions.add_argument('user-agent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"')
    chromeOptions.add_argument('--disable-extensions')
    chromeOptions.add_argument('--profile-directory=Default')
    chromeOptions.add_argument("--incognito")
    prefs = {"profile.managed_default_content_settings.images": 2}
    prefs["credentials_enable_service"] = False
    prefs["profile.password_manager_enabled"] = False
    chromeOptions.add_experimental_option("prefs", prefs)
    chromeOptions.add_experimental_option('excludeSwitches', ['enable-automation'])
    chromeOptions.add_experimental_option('w3c', False)
    return chromeOptions

下载和处理报表,需要用到pandas,修改文件名,xlsx转csv,csv转xlsx

# 修改文件名
def updateFileName(name, jobTraceUrl, jobId, projectId, downLoadPath):
    try:
        time.sleep(20)
        path = downLoadPath
        print(downLoadPath)
        if os.path.exists(os.path.join(path, name)):
            # 移除文件
            os.remove(os.path.join(path, name))
        dir_list = os.listdir(path)
        dir_list = sorted(
            dir_list, key=lambda x: os.path.getctime(os.path.join(path, x)))
        # 修改文件名
        if len(dir_list) > 0:
            os.rename(os.path.join(
                path, dir_list[-1]), os.path.join(path, name))
        jobTracePost(jobTraceUrl, jobId, name + "文件保存成功", "info", "", "", projectId)
    except Exception as e:
        return None
#xlsx转csv
def xlsx_to_csv_pd(path, fileName):
    data_xls = pd.read_excel(os.path.join(path, fileName), index_col=0)
    print(path)
    data_xls.to_csv(os.path.join(path, 'yeji.csv'), encoding='utf-8')

#csv转xlsx
def csv_to_xlsx_pd(path, fileName):
    csv = pd.read_csv(os.path.join(path, fileName), encoding='gbk')
    print(path)
    time.sleep(5)
    csv.to_excel(os.path.join(path, fileName.split(".")[0]+ ".xlsx"), sheet_name='data')
    time.sleep(5)

操作网页滑块 处理滑块验证需要引入ActionChains,模仿人工拖动滑块验证

#模仿人工拖动滑块验证
def downLoadSlideWindows(driver):
    flag = True
    flag2 = 15
    while flag and flag2 > 0:
        time.sleep(2)
        try:
            driver.find_element_by_xpath(
                "***").click()
        except Exception as e:
            print("刷新")
        finally:
            button = driver.find_element_by_id("***")
            action = ActionChains(driver)
            action.click_and_hold(button).perform()
            tracks = get_tracks(270, flag2)
            for track in tracks['forward_tracks']:
                ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform()
            time.sleep(0.3)
            ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()  # 先移动去一点
            time.sleep(0.4)
            ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform()  # 再退回来,模仿人的行为习惯
            flag2 = flag2 - 1
            time.sleep(0.6)  # 0.6秒后释放鼠标
            ActionChains(driver).release().perform()
            errDom = driver.find_elements_by_id("***")
            if len(errDom) == 0:
                flag = False

def get_tracks(distance, flag2):
        v = 0
        t = 0.15
        forward_tracks = []
        current = 0
        mid = distance * 4 / 5
        while current < distance:
            if current < mid:
                a = random.randint(200, 300)
            else:
                a = -random.randint(256, 300)
            if flag2 == 9:
                s = a * t
                current += s
                forward_tracks.append(round(s))
            elif flag2 == 7:
                v = 256
                s = v * t
                current += s
                forward_tracks.append(round(s))
            else:
                v0 = v
                s = v0 * t + 0.5 * a * (t ** 2)
                current += s
                forward_tracks.append(round(s))
                v = v0 + a * t
        return {'forward_tracks': forward_tracks}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值