python selenium Get interface data.
selenium does not provide a function to query directly, but it can be queried through the API provided by the webdriver, using the function
API documentation provided by webdriver:/devtools-protocol/tot/Network/
Documentation Description:
The parameter is requestid, which is the unique ID automatically generated by webdriver for each request, so you can get the requestid to get the content returned by the request.
How to get the requestid? When you create the webdriver object, set the configuration information to get the performance, you can get the log information of each request, and then retrieve the log information to find the corresponding requestid.
The webdriver creation code to get log information (note that configuration information must be passed in to get log information):
from selenium import webdriver from .desired_capabilities import DesiredCapabilities import time caps = { 'browserName': 'chrome', 'loggingPrefs': { 'browser': 'ALL', 'driver': 'ALL', 'performance': 'ALL', }, 'goog:chromeOptions': { 'perfLoggingPrefs': { 'enableNetwork': True, }, 'w3c': False, }, } driver = (desired_capabilities=caps) ('/union/media/login/') # You must wait a certain amount of time, otherwise you will get an error message that you can't get the log information, because you have to wait for all the requests to end before you can get the log information. (3) request_log = driver.get_log('performance')
Print request_log is an array, and then traverse request_log to retrieve the requestid corresponding to the url that needs to be fetched, such as needing to get the/bytecom/resource/union_web2/media/ corresponding requestidand get the contents of the interface:
for i in range(len(request_log)): message = (request_log[i]['message']) message = message['message']['params'] # .get() to avoid errors when the field does not exist. request = ('request') if(request is None): continue url = ('url') if(url == "/bytecom/resource/union_web2/media/"): # Get requestId print(message['requestId']) # Get interface content by requestId content = driver.execute_cdp_cmd('', {'requestId': message['requestId']}) print(content) break
Full Code:
import json from selenium import webdriver from .desired_capabilities import DesiredCapabilities import time caps = { 'browserName': 'chrome', 'loggingPrefs': { 'browser': 'ALL', 'driver': 'ALL', 'performance': 'ALL', }, 'goog:chromeOptions': { 'perfLoggingPrefs': { 'enableNetwork': True, }, 'w3c': False, }, } driver = (desired_capabilities=caps) ('/union/media/login/') # You must wait a certain amount of time, otherwise you will get an error message that you can't get the log information, because you have to wait for all the requests to end before you can get the log information. (3) request_log = driver.get_log('performance') print(request_log) for i in range(len(request_log)): message = (request_log[i]['message']) message = message['message']['params'] # .get() to avoid errors when the field does not exist. request = ('request') if(request is None): continue url = ('url') if(url == "/bytecom/resource/union_web2/media/"): # Get requestId print(message['requestId']) # Get interface content by requestId content = driver.execute_cdp_cmd('', {'requestId': message['requestId']}) print(content) break
to this article on the implementation of python selenium get interface data is introduced to this article, more related python selenium get interface data content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!