2

Below is simple code that click the login button of wp-login page. I want to print the request that is being made by selenium on button click. Is it possible using selenium. If not is there any other possible way.

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()

Below is the request that I capture on button click using burpsuite that I want to print in 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

Below solution does not work for me How can I see the entire HTTP request that's being sent by my Python application?
As I wanted to print request on button click.

  • Selenium itself isn't responsible for printing things - python is. If you're running your python script from something like [tag:py.test], it may be capturing stdout, in which case you'll need to tell it to leave stdout alone: `pytest test_foo.py -s` (the `-s` flag tells it not to capture stdout, and is synonymous with `--capture=no`) – Claire Nielsen Oct 19 '17 at 17:32

1 Answers1

0

You can obtain your goal thanks to browsermobproxy:

  1. pip install browsermob-proxy
  2. Download the file in this page
  3. Extract the zip and execute the "..browsermob-proxy-2.1.4/bin/browsermob-proxy" file (bash browsermob-proxy in Unix os)
  4. Get the path to the "..browsermob-proxy-2.1.4/bin/browsermob-proxy" file to insert in the code:

    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)
    
Davide Patti
  • 3,391
  • 2
  • 18
  • 20