[英]Python and seleniumrequests getting request headers
我需要从特定请求中获取Cookie。 问题是它是在我眼外生成的,我需要使用Selenium来模拟浏览器打开,以便我自己生成它。 第二个问题是我无法访问请求cookie。 我需要的cookie在请求中,而不是响应中。
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是通过浏览器发送到那里的,而不是作为响应发送的。 我需要拿起那块饼干。 其他图书馆也可以的话。 如果您手动转到页面,这是我需要的东西: 我选择了请求和我需要的Cookie,并显示它在请求中而不是响应中。 这有可能实现吗?
我发现这是怎么做的。 使用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
,并获取所需的cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.