我正在使用 Python 绑定运行 Selenium,并且正在尝试找出一种方法来检测 Selenium 与元素和页面交互时发送的事件。这些事件列在 Chrome 开发者工具的网络选项卡中,位于“有效负载”>“查询字符串参数”下。理想情况下,我想运行测试并打印出测试运行时发送的事件。
我尝试使用 Browsermob-proxy 捕获 HAR 数据,但我已经在运行代理,因此它为我增加了太多的复杂性。我觉得我想得太多了,考虑到我只需要以一定的时间间隔从开发工具中获取值。
我的问题的答案是硒线
然后您可以使用内置的
requests
功能来循环请求。
def event_checker(driver):
event_list = [] # Initialize event list
time.sleep(9) # Wait for the request
for request in driver.requests: # Iterate through network requests
query = request.querystring.replace('&', ' ') # Replace ampersand with space
query = query.split() # Split on space into a list of requests
for items in query: # Iterate through items in query
if "events" in items: # If the word "events" is in an item
items = items.replace('%3D', '=') # Replace unicode '=' with '='
items = items.replace('%2C', ' ') # Replace unicode ',' with space
items = items.replace('%3A', ':') # Replace unicode ':' with ':'
items = items.split() # Split on space into a list of events
for e in items: # Iterate through the list of events
if "events=" in e: # Strip "events=" label
e = e.replace('events=', '')
event_list.append(e) # Append the event to the event list
return event_list # Return the page title and event list