Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Ferrum to use default (non incognito) context #47

Closed
Pugio opened this issue Feb 12, 2020 · 5 comments · May be fixed by #471
Closed

Allow Ferrum to use default (non incognito) context #47

Pugio opened this issue Feb 12, 2020 · 5 comments · May be fixed by #471

Comments

@Pugio
Copy link

Pugio commented Feb 12, 2020

I'd like my browser state/session to be preserved between uses of my script.

Currently, Ferrum places all commands inside a BrowserContext, which acts like an Incognito session and doesn't load stored cookies.

I would like to use the browser's default session (which isn't inside a context), and take advantage of the --user-data-dir flag to be able to persist state across sessions. This would require modifying how Ferrum uses and creates contexts to allow for non-browserContext requests. I am happy to do this, and submit a PR, but would like some author feedback (if possible) about whether such an option is in line with the project's vision before I start working on code for this.

@route
Copy link
Member

route commented Feb 12, 2020

Absolutely, go ahead! I just didn’t need it thus it was easy not to implement rather than implement. I think it will be as easy as provide a path and just don’t remove anything from it when quitting.

@route
Copy link
Member

route commented Apr 7, 2020

I think the default context that Chrome creates has a lot of limitations like inability to create pages inside it and so on. If you come up with something useful please open a PR but due to inactivity I'm going to close this issue.

@route route closed this as completed Apr 7, 2020
@yann120
Copy link

yann120 commented Jun 26, 2024

Hi,
Is there an other way to persist state between sessions like we can do with Selenium ?
I came to Ferrum to be able to catch network requests, but I need also to persist the session.
Exemple, i'm logged in on a website, I want to be still logged when re-launching the script
Thanks 🙏🏻

@route
Copy link
Member

route commented Jun 29, 2024

@yann120 http is stateless protocol. The state is determined by headers sent from/to web server. If you want to login on the website, close browser and then open it and be logged in, you need to store your cookies somewhere, and next time before telling a browser to visit url, load those cookies.

You open the page and login:

browser = Ferrum::Browser.new(headless: false)
page = browser.create_page
page.go "https://auth.udacity.com/sign-in?next=https%3A%2F%2Flearn.udacity.com%2F"
page.network.wait_for_idle

page.at_xpath("//*[@id='email']").focus
page.keyboard.type(email)
page.at_xpath("//*[@id='revealable-password']").focus
page.keyboard.type(password)
button = page.at_xpath("//button[@type='button'][./span[.='Sign in']]")
button.focus.click

page.network.wait_for_idle

File.open(path, "w") do |file|
  file.write(page.cookies.map(&:to_h).to_yaml)
end

Then next time you load all the cookies before opening the page:

browser = Ferrum::Browser.new(headless: false)
page = browser.create_page

cookies = YAML.load_file(path)
cookies.each { |c| page.cookies.set(c) }
page.go "https://learn.udacity.com"

99cfa84

@yann120
Copy link

yann120 commented Sep 19, 2024

@yann120 http is stateless protocol. The state is determined by headers sent from/to web server. If you want to login on the website, close browser and then open it and be logged in, you need to store your cookies somewhere, and next time before telling a browser to visit url, load those cookies.

You open the page and login:

browser = Ferrum::Browser.new(headless: false)
page = browser.create_page
page.go "https://auth.udacity.com/sign-in?next=https%3A%2F%2Flearn.udacity.com%2F"
page.network.wait_for_idle

page.at_xpath("//*[@id='email']").focus
page.keyboard.type(email)
page.at_xpath("//*[@id='revealable-password']").focus
page.keyboard.type(password)
button = page.at_xpath("//button[@type='button'][./span[.='Sign in']]")
button.focus.click

page.network.wait_for_idle

File.open(path, "w") do |file|
  file.write(page.cookies.map(&:to_h).to_yaml)
end

Then next time you load all the cookies before opening the page:

browser = Ferrum::Browser.new(headless: false)
page = browser.create_page

cookies = YAML.load_file(path)
cookies.each { |c| page.cookies.set(c) }
page.go "https://learn.udacity.com"

99cfa84

Thanks, I ended up using your solution and it work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants