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) return 0 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返回的信息
时间: 2024-04-17 08:29:10 浏览: 99
要获取XHR返回的信息,您可以使用Selenium的`WebDriverWait`和`expected_conditions`来等待XHR请求完成,并通过`browser.execute_script()`方法执行JavaScript代码来获取XHR的响应。以下是一种可能的实现方法:
```python
import time
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()
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.click()
# 等待XHR请求完成
time.sleep(5) # 等待XHR请求完成的时间(根据实际情况调整)
# 执行JavaScript代码,获取XHR的响应
response = browser.execute_script("return performance.getEntries()[0].response")
print("XHR Response:", response)
return 0
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'])
```
在上述代码中,我们使用`time.sleep()`方法来等待XHR请求完成,然后使用`browser.execute_script()`方法执行JavaScript代码,通过`performance.getEntries()[0].response`来获取XHR的响应。请根据实际情况调整等待时间。
阅读全文