简体   繁体   English

如何在Python中使用Selenium登录后保持会话?

[英]How can I maintain session after login using Selenium in Python?

I'm succeeded logging in sites using Selenium in Python 3.4 The login process is like below: 我在Python 3.4中使用Selenium成功登录网站登录过程如下:

browser = webdriver.PhantomJS()
browser.get('url')

emailElem = self.browser.find_element_by_name('user_id')
emailElem.send_keys(user_id)

passwordElem = self.browser.find_element_by_name('password') 
passwordElem.send_keys(user_pwd)

passwordElem.submit()

After I login, I'm trying to access certain page where the log-in session is required, 登录后,我正在尝试访问需要登录会话的某个页面,

browser.get('login-session-required-page-url')

It lost its session and not allowed to access. 它失去了会话而不允许访问。

I think that this is related with session and cookies, but I have no idea how to deal with it. 我认为这与会话和cookie有关,但我不知道如何处理它。

Is your String Object inside your browser.get parenthesis a functional url after you have successfully logged in regularly on a mainstream browser? 在主流浏览器上成功登录后,您的browser.get括号内的String对象是否为功能URL?

If you were to login manually instead of through selenium, the new URL that you want to access (the one which requires that the User is Logged In), should be the String URL inside your browser.get . 如果您要手动登录而不是通过selenium登录,则要访问的新URL(要求登录用户的URL)应该是browser.get的String URL。

There are a few potential issues with your code. 您的代码存在一些潜在问题。 You did not specify any waiting time in between submitting through submit() and the next browser.get() . 您没有指定通过submit()和下一个browser.get() submit()之间的任何等待时间。

Consider adding a 10 to 30 second wait in between like so : 考虑在两者之间添加10到30秒的等待,如下所示:

passwordElem.submit()

browser.implicitly_wait(10)

browser.get('https://www.YourSite.com/LoggedIn/User/SomeNewPageThatRequiresBeingSignedIn/')

Another possibility is that the URL you are accessing after Logging in is unique to the cookies you were given only at the most recent logged in session. 另一种可能性是,您在登录后访问的URL对于仅在最近登录的会话中提供的cookie是唯一的。 Try to see if the URL is exactly the same over multiple logins through different browsers at different times. 尝试在不同时间通过不同浏览器查看多个登录时URL是否完全相同。 It may be that you are issued a unique URL every time you Login. 您可能每次登录时都会获得一个唯一的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 selenium-python 中维护登录 session? - Is it possible to maintain login session in selenium-python? 如何使用 python selenium 登录? - How can I login using python selenium? 如何使用Python脚本登录时维护登录会话 - How to maintain login session while logging in using Python Script Python:维护会话以便在登录后访问所有页面 - Python : Maintain the session in order to access all the pages after login 与Scrapy进行身份验证的登录会话后使用Selenium - Using Selenium after authenticated login session with Scrapy 使用 selenium 登录到 stackoverflow 可以工作,但使用 scrapy python 不行。如何使用无头浏览登录? - Login to stackoverflow using selenium is working but using scrapy python is not.How can I login with headless browsing? 使用 selenium 和 python 登录后,我无法导航到 Instagram 页面 - I can't navigate to instagram pages after login using selenium and python 如何在没有驱动器 API 的情况下使用 selenium 和 python 登录谷歌驱动器 - How can I login to google drive using selenium & python WITHOUT Drive API 我如何使用python和selenium自动登录网站 - how do i automatically login into website using python and selenium 如何编写可登录该网站并维护会话信息的python脚本? - How to write a python script that can log into this website and maintain session information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM