2017-10-19 43 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)