browser = webdriver.Edge() browser.get(url) # 创建一个浏览器实例 driver = webdriver.EdgeOptions() driver.add_experimental_option('detach', True) # 启动一个新的SeleniumWire请求拦截器 driver.request_interceptor = lambda request: request # 等待用户名输入框可见 username_input = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'username'))) # 找到用户名输入框并输入用户名,通过xPath来定位用户名位置,谷歌端使用右键复制xPath # username_input = browser.find_element(by=By.ID,value='username') username_input.send_keys(username) # 找到密码输入框并输入密码 password_input = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'password'))) password_input.send_keys(password) # 模拟回车键,提交表单 login_btn = browser.find_element(by=By.ID, value='denglub') login_btn.send_keys(Keys.ENTER) 之后我该如何获取他的HDR值、响应值、cookie等信息
时间: 2024-04-17 12:29:40 浏览: 204
您使用SeleniumWire来获取请求和响应的信息。以下是一个示例代码,演示如何获取请求的HDR值、响应值、cookie等信息:
```python
import seleniumwire
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建一个浏览器实例
options = webdriver.EdgeOptions()
options.add_experimental_option('detach', True)
# 启动一个新的SeleniumWire请求拦截器
seleniumwire_options = {
'backend': 'mitmproxy'
}
driver = seleniumwire.webdriver.Edge(options=options, seleniumwire_options=seleniumwire_options)
url = 'https://example.com'
browser.get(url)
# 等待用户名输入框可见
username_input = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'username')))
# 找到用户名输入框并输入用户名
username_input.send_keys(username)
# 找到密码输入框并输入密码
password_input = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'password')))
password_input.send_keys(password)
# 模拟回车键,提交表单
login_btn = browser.find_element(by=By.ID, value='denglub')
login_btn.send_keys(Keys.ENTER)
# 获取所有的请求
requests = driver.requests
# 遍历请求并获取所需信息
for request in requests:
# 获取请求的URL
url = request.url
# 获取请求的方法,例如GET、POST等
method = request.method
# 获取请求的头部信息
headers = request.headers
# 获取请求的参数
params = request.params
# 获取请求的响应
response = request.response
# 获取响应的状态码
status_code = response.status_code
# 获取响应的头部信息
response_headers = response.headers
# 获取响应的内容
content = response.body
# 获取响应的cookie
cookies = response.cookies
# 打印请求和响应信息
print(f"URL: {url}")
print(f"Method: {method}")
print(f"Headers: {headers}")
print(f"Params: {params}")
print(f"Status Code: {status_code}")
print(f"Response Headers: {response_headers}")
print(f"Content: {content}")
print(f"Cookies: {cookies}")
# 关闭浏览器
browser.quit()
```
请注意,您需要安装`SeleniumWire`库和相应的浏览器驱动程序(如`msedgedriver`)来运行此代码。此外,您还需要安装`mitmproxy`来启动SeleniumWire拦截器。
希望这可以帮助到您!如果您有任何其他问题,请随时提问。
阅读全文