我正在运行执行以下操作的 Python 脚本:
- 打开网页
- 使用登录表格登录游戏
- 等待页面加载(获取 Ajax 请求的 Json 响应)
- 执行几次点击
- 单击触发 ajax 请求的按钮
- 需要读取 Ajax 请求的 json 响应。<这就是我的问题所在
- 写入文件
我的问题是我无法捕获第二轮 Ajax 请求,并且在谷歌搜索时找不到任何解决方案。
我的代码如下所示:
import sys
import json
import time
from seleniumwire import webdriver # Import from seleniumwire
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r'C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get(myURL)
# Login code here
loginButton = driver.find_element_by_id('login_Login')
loginButton.click();
for request in driver.requests: # Initial page loading
if "login_check" in request.path:
if request.response:
response_body = json.loads(request.response.body)
for key, value in response_body.items():
if key == 'success':
if value is False:
# do some stuff and write to file
#driver.quit()
break
else:
defaultContent_switched = driver.switch_to.default_content()
try:
# do some stuff and write to file
except TimeoutException:
canvas = driver.find_element_by_tag_name('canvas')
canvas.click();
#click on a few elements
for request in driver.requests:
if "/game/json" in request.path:
print(request.path); # At this point I only see the requests of the initial load of the page
if request.response:
# Need to do stuff here...
break
break
break
print(json.dumps(output))
driver.quit()
单击画布后如何重新开始收听网络?