如何使用Selenium两次访问同一网站而不会丢失设置?

发布于 2025-02-10 08:03:03 字数 724 浏览 3 评论 0原文

我访问网站,登录,然后不经历查找和写作的过程,而是我认为我只是通过我想要的搜索查询来重新访问网站。

问题是,当我使用第二个“驱动程序”(下面的代码中的最后一行代码)访问网站时,好像忘记了我以前登录了;好像这是我开启的全新会议。

我有此代码结构:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

path = Service("C://chromedriver.exe")
driver = webdriver.Chrome(service=path)

driver.get('https://testwebsite.com/')

login_email_button = driver.find_element(By.XPATH,'XXXXX')
login_email_button.click()

username = driver.find_element(By.ID, 'email')
password = driver.find_element(By.ID, 'password')

username.send_keys('myuser')
password.send_keys('mypassword')

driver.get('https://testwebsite.com/search?=televisions')

I access a website, login and then instead of going through the process of finding and writing into the website's search field, I thought I'd simply re-access the website through a URL with the search query I want.

The problem is that when I access the website with the second "driver.get" (last line of code in the code below), it's as though it forgets that I logged in previously; as though it was a totally new session that I opened.

I have this code structure:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

path = Service("C://chromedriver.exe")
driver = webdriver.Chrome(service=path)

driver.get('https://testwebsite.com/')

login_email_button = driver.find_element(By.XPATH,'XXXXX')
login_email_button.click()

username = driver.find_element(By.ID, 'email')
password = driver.find_element(By.ID, 'password')

username.send_keys('myuser')
password.send_keys('mypassword')

driver.get('https://testwebsite.com/search?=televisions')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2025-02-17 08:03:03

当您进行

driver.get('https://testwebsite.com/search?=televisions')

您正在开启新的会话,而没有cookie或上一个会话的数据。您可以尝试重复选项卡,以保持

登录

url = driver.current_url
driver.execute_script(f"window.open('{url}');") 
driver.switch_to.window(window_handles[1]) 

# if you want give a name to tab, pass it as second param like
driver.execute_script(f"window.open('{url}', 'second_tab_name');")
driver.switch_to.window('second_tab_name')

when you do

driver.get('https://testwebsite.com/search?=televisions')

you're opening new session with no cookie or data of previous session. You can try to duplicate tab instead, to keep you logged in. You can do with:

Driver.execute_script

url = driver.current_url
driver.execute_script(f"window.open('{url}');") 
driver.switch_to.window(window_handles[1]) 

# if you want give a name to tab, pass it as second param like
driver.execute_script(f"window.open('{url}', 'second_tab_name');")
driver.switch_to.window('second_tab_name')

remember to use the switch if you want go back to the main tab

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文