import logging from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
时间: 2023-06-15 12:05:47 浏览: 162
这段代码是用于导入 Python 中的 logging 模块以及 Selenium 的 DesiredCapabilities 类。logging 模块用于记录程序运行时的日志,而 DesiredCapabilities 类则用于设置浏览器的各种参数和选项,例如浏览器类型、版本、语言、是否启用 JavaScript 等等。一般来说,在使用 Selenium 进行自动化测试时,需要先创建一个 DesiredCapabilities 对象,并设置好所需的参数,然后再将该对象传递给 webdriver.Remote() 方法,以启动一个远程浏览器实例。
相关问题
webdriver.Chrome() 怎么设置请求次数
您可以使用以下代码来设置请求次数:
```python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-infobars')
options.add_argument('--disable-notifications')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-web-security')
options.add_argument('--disable-logging')
options.add_argument('--log-level=3')
driver = webdriver.Chrome(desired_capabilities=caps, options=options)
driver.execute_cdp_cmd('Network.enable', {})
driver.execute_cdp_cmd('Network.setCacheDisabled', {'cacheDisabled': True})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Referer': 'https://www.google.com/'}})
driver.execute_cdp_cmd('Network.setRequestInterception', {'patterns': [{'urlPattern': '*', 'resourceType': 'Document', 'interceptionStage': 'HeadersReceived'}]})
driver.execute_cdp_cmd('Network.interceptRequest', {'urlPattern': '*', 'resourceType': 'Document', 'interceptionStage': 'HeadersReceived', 'handler': 'Network.continueInterceptedRequest'})
driver.execute_cdp_cmd('Network.setBlockedURLs', {'urls': ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.css', '*.js', '*.svg', '*.woff', '*.woff2', '*.ttf', '*.eot', '*.ico']})
driver.execute_cdp_cmd('Network.setCookie', {'name': 'cookie_name', 'value': 'cookie_value', 'url': 'https://www.google.com/'})
driver.execute_cdp_cmd('Network.setCookies', {'cookies': [{'name': 'cookie_name', 'value': 'cookie_value', 'url': 'https://www.google.com/'}]})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Referer': 'https://www.google.com/'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Accept-Language': 'en-US,en;q=0.9'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Accept-Encoding': 'gzip, deflate, br'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Connection': 'keep-alive'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Upgrade-Insecure-Requests': '1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Cache-Control': 'max-age=0'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Pragma': 'no-cache'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'TE': 'Trailers'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'DNT': '1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Requested-With': 'XMLHttpRequest'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port': '443'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host-Original': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host-Original': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto-Original': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme-Original': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port-Original': '443'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host-Client-IP': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host-Client-IP': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto-Client-IP': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme-Client-IP': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port-Client-IP': '443'}})
url = 'https://www.google.com/'
for i in range(10):
driver.get(url)
```
这段代码可以设置请求次数为10次,您可以根据需要进行修改。
selenium打开edge时,怎么和chrome一样进行edge的初始设置
在Selenium中,如果你想要像操作Chrome浏览器那样初始化Edge浏览器,并进行一些预设的设置,你需要使用WebDriverManager来管理浏览器驱动。WebDriverManager是一个方便的工具库,可以自动下载并管理浏览器驱动,包括Microsoft Edge。
以下是使用Python示例代码:
```python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# 创建Edge选项
options = Options()
options.add_argument('--edge-launch-mode=hub') # 设置启动模式为边缘浏览器的Hub模式
options.add_experimental_option('excludeSwitches', ['enable-logging']) # 关闭日志记录,以避免输出过多信息
# 如果需要,你可以添加特定的配置,如语言、代理等
options.set_edge_user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30")
# 初始化Driver
driver = webdriver.Edge(service_log_path=None, options=options)
driver.get("https://www.example.com") # 打开初始URL
# 进行你的页面操作...
# ...
# 关闭浏览器
driver.quit()
阅读全文