由selenium web驱动程序发出的打印请求

时间:2017-10-19 07:53:41

标签: python selenium request

下面是单击wp-login页面登录按钮的简单代码。我想打印按钮点击时由selenium发出的请求。是否可以使用硒。如果没有,还有其他可能的方式。

def init_driver():
    driver = webdriver.PhantomJS()
    driver.wait = WebDriverWait(driver, 5)
    return driver


def clickButton(driver):
    driver.get("http://website.com/wp-login.php")
    try:
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.NAME, "wp-submit")))
        button.click()

    except TimeoutException:
        print("Button not found in google.com")

if __name__ == "__main__":
    driver = init_driver()
    clickButton(driver)
    driver.quit()

下面是我使用burpsuite按钮点击捕获的请求,我想在python中打印:

POST /wp-login.php HTTP/1.1
Host: website.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Referer: http://website.com/wp-login.php
Cookie: wordpress_test_cookie=WP+Cookie+check; AWSELB=7DdsfsdfsdsfgeredfgfebfgrehgfdcxBD20E6
Connection: close
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
Content-Length: 94


log=&pwd=&wp-submit=Log+In&redirect_to=http%3A%2F%2Fwebsite.com%2Fwp-admin%2F&testcookie=1

以下解决方案对我不起作用 How can I see the entire HTTP request that's being sent by my Python application?
因为我想按钮点击打印请求。

1 个答案:

答案 0 :(得分:0)

感谢browsermobproxy

,您可以获得目标
  1. pip install browsermob-proxy
  2. 在此page
  3. 下载文件
  4. 解压缩并执行“..browsermob-proxy-2.1.4 / bin / browsermob-proxy”文件(在Unix操作系统中为bash browsermob-proxy
  5. 获取“..browsermob-proxy-2.1.4 / bin / browsermob-proxy”文件的路径以在代码中插入:

    from selenium import webdriver
    from browsermobproxy import Server
    
    server = Server("/pathTo/browsermob-proxy-2.1.4/bin/browsermob-proxy")
    server.start()
    proxy = server.create_proxy({'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True})
    
    service_args = ["--proxy=%s" % proxy.proxy, '--ignore-ssl-errors=yes']
    driver = webdriver.PhantomJS(service_args=service_args)
    
    proxy.new_har()
    driver.get('https://www.google.co.uk/')
    print(proxy.har)  # this is the archive
    # for example:
    all_url_requests = [entry['request']['url'] for entry in proxy.har['log']['entries']]
    print(all_url_requests)
    
相关问题