user16334850
user16334850

Reputation:

How to redirect to a link using selenium

Hi I want to redirect to a link but I want to save the cookies too

is there any way to save cookies and redirect to a URL

required_url = driver.find_element_by_xpath("//*[@id='main-content']/div[1]/table/tbody/tr[1]/td[1]/a").get_attribute("href")

this required_url has a link and I want to redirect to this link

Upvotes: 1

Views: 569

Answers (1)

The Acturial Kid
The Acturial Kid

Reputation: 232

For saving cookies, Use pickle as following.

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.example.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

And later to add them back:

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.example.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)

and for redirecting to another URL

required_url = driver.find_element_by_xpath("//*[@id='main-content']/div[1]/table/tbody/tr[1]/td[1]/a").get_attribute("href")
driver.get(required_url)

Upvotes: 1

Related Questions