赞
踩
- import socket
- import sys
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- import time
- import random
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as ec
- from selenium.webdriver.common.action_chains import ActionChains
- # from selenium.webdriver.chrome.options import Options
- from selenium.webdriver.chrome.service import Service
- import uiautomation as auto
- # import pyperclip
- import traceback
- from selenium.webdriver.common.keys import Keys
- from datetime import datetime
- from dateutil.relativedelta import relativedelta
- import os
- import requests
- import csv

大部分URL都是https开头,但是遇到http开头的URL的时候,浏览器很可能会把http自动识别成https,导致无法进入正确的URL。使用driver.get()就会经常遇到这种情况,遇到这种情况我使用的方式是让uiautomation输入一遍完整的URL,避免浏览器或者driver.get()函数自动补全http为https。
- port = 49152
- my_driver_path = r"G:\Py\spider\venv\Scripts\chromedriver.exe"
- # my_driver_path = r"E:\Python\AutoUI\venv\Scripts\chromedriver.exe"
- # 获取当前脚本的绝对路径
- script_path = os.path.abspath(__file__)
- # 获取当前脚本的全名(包括路径和扩展名)
- script_fullname = os.path.basename(script_path)
- download_path = script_path.replace(f"{script_fullname}", "AutomationProfile")
- web_tittle = "88888888"
- target_url = '88888888'
- if is_port_in_use(port):
- print(f"Port {port} is in use")
- else:
- print(f"Port {port} is available")
- # 设置Chrome选项,以启用远程调试并指定一个端口
- chrome_options = webdriver.ChromeOptions()
- chrome_options.add_experimental_option("detach", True)
- chrome_options.add_argument('--disable-web-security')
- chrome_options.add_argument('--allow-running-insecure-content')
- chrome_options.add_argument("--ignore-certificate-errors")
- chrome_options.add_argument(f"--remote-debugging-port={port}")
- chrome_options.add_argument("--disable-infobars")
- prefs = {
- "download.default_directory": f"{download_path}",
- "download.prompt_for_download": False,
- "download.directory_upgrade": True,
- "safebrowsing.enabled": False
- }
- chrome_options.add_experimental_option("prefs", prefs)
- # 创建webdriver服务实例,指定chromedriver的路径
- service = Service(executable_path=my_driver_path)
- # 创建webdriver实例,并传入定义的选项
- driver = webdriver.Chrome(service=service, options=chrome_options)
- # 设置页面加载超时时间
- driver.set_page_load_timeout(10)
- ac = ActionChains(driver)

显然我这里用的是谷歌浏览器,指定了浏览器驱动的位置,指定了自动化测试浏览器使用的端口,指定端口的好处就是可以让其他脚本来接管同一个自动化测试浏览器,指定了谷歌浏览器文件下载的位置,同时把谷歌浏览器的detach设置为True,意思是这个脚本创建启动了一个自动化测试的浏览器实例之后与该浏览器分离,这样在脚本运行结束之后,浏览器就不会被关闭了,可以一直存在,直到自己手动去关闭这个浏览器。
我还给浏览器设置了一些可能有用的设置,有一部分可能是不生效的,但是不影响。
我常用的用于更新谷歌浏览器驱动的网址是这个: ChromeDriver - WebDriver for Chrome
这个网址提供的谷歌浏览器驱动非常全面了。
如果死活找不到某个元素,那么很可能是某个元素位于某个iframe中,或者某个ifram的嵌套的嵌套的嵌套的iframe中。这种情况使用 driver.switch_to.frame(target_elements) 即可。
- def switch_frame_id(id):
- target_elements = find_elements_id(id)
- driver.switch_to.frame(target_elements)
不一定是通过ID,也可以是通过CLASS等其他特征
那么你就可以把空白页先关闭,同时切换到指定名称的标签页
- def close_about_blank():
- global driver
- window_handles = driver.window_handles
- # print(driver.title)
- # 获取当前所有打开的窗口的句柄
- # print(window_handles)
- for handle in window_handles:
- driver.switch_to.window(handle)
- # print(driver.title)
- if driver.title == "":
- driver.close()
- continue
-
-
- def get_target_title():
- global driver
- window_handles = driver.window_handles
- # print(driver.title)
- # 获取当前所有打开的窗口的句柄
- # print(window_handles)
- for handle in window_handles:
- driver.switch_to.window(handle)
- # print(driver.title)
- if driver.title == web_tittle:
- break
- # print(driver.title)

如果害怕这个问题造成的困扰,你可以在每次使用这个tag之前重新再获取一次这个tag
获取tag的时候,最保险的是使用find_elements,这样的话不管能不能找到该tag,它都会返回一个列表,即使这个列表是空的,也不会爆红,如果是find_element遇到找不到该元素的情况,就会立马爆红,程序提前终止(如果没有捕获错误的相应措施的话)。
我们在启动浏览器的时候,指定了一个端口号,那么接管那个自动化测试浏览器的时候,用回那个端口号就可以了
- def down_file(
- accept_date_start,
- accept_date_end):
- # 设置Chrome选项,以连接到远程调试端口
- global chrome_options
- global service
- global driver
- global ac
- global start_date
- global end_date
- chrome_options = webdriver.ChromeOptions()
- chrome_options.add_experimental_option(f"debuggerAddress", f"127.0.0.1:{port}")
- # 创建webdriver服务实例,指定chromedriver的路径
- service = Service(executable_path=my_driver_path)
- # 创建webdriver实例,并传入定义的选项,这将接管现有的浏览器实例
- driver = webdriver.Chrome(service=service, options=chrome_options)
- # 设置页面加载超时时间
- driver.set_page_load_timeout(3)
- # driver = webdriver.Chrome(options=chrome_options)
- ac = ActionChains(driver)

不要觉得30秒才下载一个文件好像很慢,在你不使用IP代理池的时候,如果同一个IP在半小内或者一小时内下载的内容量过大时,服务器会送你一个502 Bad Gateway。又或者导致服务器限制你的流量或者把你拉黑。
一般来说,慢就是快,越快就是越慢,越慢就是越快。
熟练使用IP代理池当我没说,hhh
以上这些方法基本够用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。