2017-10-19 17 views
2

下面是简单的代码,点击wp-login页面的登录按钮。我想打印由硒按钮点击进行的请求。是否有可能使用硒。如果没有其他可能的方法。selenium web驱动程序的打印请求

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?
因为我想打印工作请点击按钮。

+0

硒本身不负责印刷的东西 - 蟒蛇是。如果你使用[tag:py.test]来运行你的python脚本,它可能会捕获stdout,在这种情况下,你需要告诉它只保留stdout:'pytest test_foo.py -s' '-s'标志告诉它不要捕获标准输出,并且与'--capture = no'同义] –

回答

0

你可以得到你的目标得益于browsermobproxy

  1. pip install browsermob-proxy
  2. 下载文件中的这个page
  3. 解压ZIP和执行” ..browsermob代理-2.1.4/bin中/ browsermob-proxy“文件(在Unix操作系统中为​​)
  4. 获取要插入代码中的”..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)