Javascript Selenium在调用document.location时释放会话

Javascript Selenium在调用document.location时释放会话,javascript,google-chrome,selenium,session,webdriver,Javascript,Google Chrome,Selenium,Session,Webdriver,我使用带Selenium的Chrome webdriver登录网站。 当我让它点击一个href是Javascript函数“document.location”的链接时,它显然会松开会话,因为它被重定向到登录页面 <a href="javascript:document.location='/'+'some-location'+'/'">A link</a> 在我看来,这很可能不是一个bug,而是一个预期的行为,没有我的具体干预,但我找不到如何防止它 编辑: 从sel

我使用带Selenium的Chrome webdriver登录网站。 当我让它点击一个href是Javascript函数“document.location”的链接时,它显然会松开会话,因为它被重定向到登录页面

<a href="javascript:document.location='/'+'some-location'+'/'">A link</a>

在我看来,这很可能不是一个bug,而是一个预期的行为,没有我的具体干预,但我找不到如何防止它

编辑:

从selenium导入webdriver
从fake_useragent导入useragent
导入时间
随机输入
罗吉努尔酒店https://www.website.com/myads/'
账户=[
[“login1”,“pass1”],
[“login2”,“pass2”]
]
打印(“生成浏览器”)
user\u agent=UserAgent().random
打印(“使用用户代理:+用户代理”)
chrome\u options=webdriver.ChromeOptions()
chrome\u选项。添加\u参数(“--headless”)
chrome\u选项。添加\u参数(“--nogui”)
chrome\u选项。添加\u参数(“--nosandbox”)
chrome_选项。添加_参数(f'user-agent={user_-agent}')
浏览器=webdriver.Chrome(Chrome\u选项=Chrome\u选项)
i=0
而我(账户):
打印(“进入登录页面”)
browser.get(loginurl)
打印(“等待…”)
时间。睡眠(随机。均匀(2,4))
打印(“填写登录表”)
打印(“电子邮件:+帐户[i][0])
电子邮件输入=浏览器。通过id(“电子邮件”)查找元素
电子邮件输入。发送密钥(帐户[i][0])
打印(“等待…”)
时间。睡眠(随机。均匀(2,4))
打印(“密码:+帐户[i][1])
密码\u输入=浏览器。通过\u id(“密码”)查找\u元素
密码输入。发送密钥(帐户[i][1])
打印(“等待…”)
时间。睡眠(随机。均匀(2,4))
打印(“登录”)
提交=浏览器。按类名称(“提交”)查找元素。单击()
cookies=browser.manage().get_cookies()
打印(“等待…”)
时间。睡眠(随机。均匀(2,4))
打印(“进入我的广告”)
浏览器。通过“css”选择器(“a[href*='myads']”查找元素。单击()
# ...

为了匹配文档中的链接,我使用了一种模式,该模式实际上匹配了几个链接,其中几个链接的目的是注销


时不时地呼吸新鲜空气是关键。

你能给我们看看你的代码吗?你是如何点击链接的?请问您的密码是多少?因为它是重定向到登录页面,所以我们想知道,当您手动编辑登录页面时,它会在新选项卡或新窗口中打开。谢谢。我只在命令行中测试过,我打印了页面的输出,我识别了登录页面。我刚刚了解到,我可以从Python cli命令一个实际的chrome页面。我在测试,是的。谢谢你,很抱歉占用你的时间。
from selenium import webdriver
from fake_useragent import UserAgent

import time
import random

loginurl = 'https://www.website.com/myads/'

accounts = [
["login1", "pass1"],
["login2", "pass2"]
]


print("Generating the browser.")

user_agent = UserAgent().random
print("Using user agent : " + user_agent)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless") 
chrome_options.add_argument("--nogui")
chrome_options.add_argument("--nosandbox") 
chrome_options.add_argument(f'user-agent={user_agent}')

browser = webdriver.Chrome(chrome_options=chrome_options)

i = 0


while i < len(accounts):
    print("Getting to the login page.")
    browser.get(loginurl)

    print("Waiting...")
    time.sleep(random.uniform(2, 4))

    print("Filling login form.")

    print("Email : " + accounts[i][0])
    email_input = browser.find_element_by_id("email")
    email_input.send_keys(accounts[i][0])

    print("Waiting...")
    time.sleep(random.uniform(2, 4))

    print("Password : " + accounts[i][1])
    password_input = browser.find_element_by_id("password")
    password_input.send_keys(accounts[i][1])

    print("Waiting...")
    time.sleep(random.uniform(2, 4))

    print("Loggin in.")
    submit = browser.find_element_by_class_name("submit").click()
    cookies = browser.manage().get_cookies()

    print("Waiting...")
    time.sleep(random.uniform(2, 4))

    print("Getting to my ads.")
    browser.find_element_by_css_selector("a[href*='myads']").click()

    # ...