[英]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.