import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def login_bp(url, username, password): # 创建一个浏览器实例 options = webdriver.EdgeOptions() options.add_experimental_option('detach', True) # 打开登录页面 browser = webdriver.Edge() 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) # 获取cookie信息 cookies = browser.get_cookies() print("Cookies:", cookies) # 获取响应体和载荷信息 page_source = browser.page_source current_url = browser.current_url print("Page Source:", page_source) print("Current URL:", current_url) if __name__ == '__main__': # 定义目标信息 atr_url = { 'url': 'http://pfs.ikhtech.com/FAPI/login.jsp', 'username': 'zhangwei', 'password': 'zhangwei' } login_bp(atr_url['url'], atr_url['username'], atr_url['password']) 添加一个能够获取到登陆页登录时XHR返回的post请求信息
时间: 2024-04-17 22:29:14 浏览: 216
selenium.webdriver.4.1.0.nupkg
要获取登录页登录时XHR返回的POST请求信息,你可以使用Selenium的`browser.get_log('performance')`方法来获取浏览器的性能日志,然后从性能日志中筛选出XHR请求信息。
以下是一个示例代码,展示了如何获取登录页登录时的POST请求信息:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def login_bp(url, username, password):
# 创建一个浏览器实例
options = webdriver.EdgeOptions()
options.add_experimental_option('detach', True)
# 打开登录页面
browser = webdriver.Edge(options=options)
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 = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'denglub')))
login_btn.click()
# 获取性能日志
logs = browser.get_log('performance')
# 筛选出XHR请求信息
xhr_logs = [log['message'] for log in logs if 'Network.requestWillBeSent' in log['message']]
for xhr_log in xhr_logs:
print("XHR Request:", xhr_log)
# 关闭浏览器
browser.quit()
if __name__ == '__main__':
# 定义目标信息
atr_url = {
'url': 'http://pfs.ikhtech.com/FAPI/login.jsp',
'username': 'zhangwei',
'password': 'zhangwei'
}
login_bp(atr_url['url'], atr_url['username'], atr_url['password'])
```
在上面的示例代码中,首先创建了一个Edge浏览器实例,然后打开登录页面。接着使用`WebDriverWait`等待用户名输入框和密码输入框可见,并分别输入用户名和密码。然后等待登录按钮可见,并点击登录按钮。
接下来,使用`browser.get_log('performance')`方法获取浏览器的性能日志。然后使用列表推导式筛选出性能日志中包含XHR请求信息的日志,并打印出来。
最后关闭浏览器。你可以根据需要修改代码,以适应不同的登录页面和获取不同的XHR请求信息。
阅读全文