2017-07-24 29 views
1

我需要從一個特定的請求獲得一個cookie。問題是它在我眼睛外面產生,我需要使用Selenium來模擬瀏覽器打開,所以我可以自己生成它。第二個問題是我無法訪問請求cookie。我需要的cookie是在請求中,而不是響應。Python和seleniumrequests獲得請求頭

from selenium import webdriver 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 

binary = FirefoxBinary('/usr/bin/firefox') 
driver = webdriver.Firefox(firefox_binary=binary) 
driver.get('http://www.princess.com/find/searchResults.do') 
driver.find_elements_by_xpath('//*[@id="LSV010"]/div[3]/div[1]/div[1]/button')[0].click() 

此代碼塊打開頁面,然後點擊「查看所有日期和定價」鏈接。 cookie是通過瀏覽器發送的,而不是迴應。我需要把握我的手。如果他們可以完成這項工作,其他圖書館也可以。 如果手動去的網頁,這是我需要的東西:我選擇的請求和餅乾,我需要 enter image description here 並且因爲它顯示了它在請求不響應。這有可能實現嗎?

+0

所以你想檢查cookie? –

+0

cookie在瀏覽器發送的請求中。 '.click()'調用的URL是'http:// www.princess.com/find/viewAllCruises.do',我需要的Cookie始終以'_aeu = QCQ =;'開頭。 – Nephilimrising

+0

你需要向網站發送一個具體cookie的請求,或者你想檢查cookie嗎?你的要求是什麼?抱歉,但它真的很混亂。 –

回答

0

我發現如何做到這一點。使用selenium庫我還是設法得到這個工作:

def fix_header(): 
    browser = webdriver.Firefox(executable_path='geckodriver.exe', firefox_profile=profile) 
    browser.get('https://www.princess.com/find/searchResults.do') 
    browser.find_element_by_xpath(
     "//*[@class='expand-table view-all-link util-link plain-text-btn gotham-bold']") 
    WebDriverWait(browser, 60).until(EC.visibility_of_any_elements_located(
     (By.XPATH, "//*[@class='expand-table view-all-link util-link plain-text-btn gotham-bold']"))) 
    try: 
     browser.find_element_by_xpath(
      "//*[@class='expand-table view-all-link util-link plain-text-btn gotham-bold']").click() 
    except Exception: 
     browser.find_element_by_class_name('mfp-close').click() 
     browser.find_element_by_xpath(
      "//*[@class='expand-table view-all-link util-link plain-text-btn gotham-bold']").click() 

    cookies = browser.get_cookies() 
    browser.close() 
    chrome_cookie = '' 
    for c in cookies: 
     chrome_cookie += c['name'] 
     chrome_cookie += '=' 
     chrome_cookie += c['value'] 
     chrome_cookie += "; " 
    return chrome_cookie[:-2] 

Selenium實際上轉到頁和「點擊」的url我需要一個瀏覽器,並得到所需要的餅乾。