Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium Webdriver登录到某个网站失败,而用户成功登录_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python Selenium Webdriver登录到某个网站失败,而用户成功登录

Python Selenium Webdriver登录到某个网站失败,而用户成功登录,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在编写一个应用程序,自动汇总我们在多个家电销售商(如ao.com和currys.co.uk)之间的订单 在其中一个网站currys.co.uk上,我们遇到了一个问题。在本例中,在mac上使用Selenium Chrome登录WebDriver似乎是不可能的,但据我所知,浏览器中的人确实可以执行完全相同的操作 是URL 我稍微简化了它,以删除实际使用中可能需要的大多数条件逻辑或等待逻辑 选项=webdriver.ChromeOptions 选项。添加参数'window-size=1200x60

我正在编写一个应用程序,自动汇总我们在多个家电销售商(如ao.com和currys.co.uk)之间的订单

在其中一个网站currys.co.uk上,我们遇到了一个问题。在本例中,在mac上使用Selenium Chrome登录WebDriver似乎是不可能的,但据我所知,浏览器中的人确实可以执行完全相同的操作

是URL

我稍微简化了它,以删除实际使用中可能需要的大多数条件逻辑或等待逻辑

选项=webdriver.ChromeOptions 选项。添加参数'window-size=1200x600' options.add_argument'user-agent=Mozilla/5.0 Macintosh;英特尔Mac OS X 10_15_4 AppleWebKit/537.36 KHTML,如Gecko Chrome/88.0.4324.192 Safari/537.36' driver=webdriver.Chromeoptions=options 转到登录页 驾驶员gethttps://www.currys.co.uk/gbuk/s/authentication.html 关闭cookie弹出窗口(如果显示) 尝试: WebDriverWaitdriver,10.untelec.element\u to\u be\u clickableBy.ID,onetrust-accept-btn-handler.click 除: 通过 输入电子邮件地址 driver.find_elementBy.ID,input-sEmail.send_keysexample@example.com 输入密码 driver.find_elementBy.ID,input-sPassword.send_键已编辑 选择“记住我” driver.find_elementBy.CSS_SELECTOR、.dc-checkbox.click 获取提交按钮 submit_button=WebDriverWaitdriver,10.untelec.element_to_be_clickable by.CSS_SELECTOR.dc按钮 点击提交 提交按钮。单击 使用返回键提交 driver.find_elementBy.ID,input-sPassword.send_keysKeys.RETURN 与JS一起提交 驱动程序。执行脚本参数[0]。单击;,提交按钮 奇怪的是,我尝试的3种提交方法中的任何一种似乎都是提交的,因为它会导致302。这也是由人工进行登录时发生的情况。但是,用户未登录,webdriver再次加载相同的身份验证页面,并且没有错误或警告

相反,如果WebDriver生成的Chrome实例中有人单击submit按钮,则用户已登录。我不明白为什么会这样

与此相关的可能是,在Selenium中,即使我将选项中的user data dir设置为不用于任何其他用途的配置文件,在浏览器会话之间也永远不会记住用户登录。。。即使有人登录。这与我使用普通的Chrome浏览器登录时发生的情况形成对比

options.add_argumentf'user-data-dir='/tmp/profile}'
我能得出的唯一结论是,咖喱在他们这方面所做的一些事情阻止了这一点,但我不希望这样,否则该应用程序将无法在这一网站上运行。

我看了一下,我同意这是相当奇怪的。我有理论解释为什么它不起作用,但我似乎无法证明

好消息是有一个解决办法。登录两次

试试这个:

driver = webdriver.Chrome()

# Goto login page
driver.get("https://www.currys.co.uk/gbuk/s/authentication.html")
# Close cookie popup if shown
try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
except:
    pass
# Enter email address
driver.find_element(By.ID, "input-sEmail").send_keys("example@example.com")
# Enter password
driver.find_element(By.ID, "input-sPassword").send_keys("redacted")
# Select remember-me
driver.find_element(By.CSS_SELECTOR, ".dc-checkbox").click()
# Get submit button
submit_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dc-button")))
# Submit with click
submit_button.click()


#dont need this twice - remembers friom previous run
#driver.find_element(By.ID, "input-sEmail").send_keys("example@example.com")
driver.find_element(By.ID, "input-sPassword").send_keys("redacted")
driver.find_element(By.CSS_SELECTOR, ".dc-checkbox").click()
submit_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dc-button")))
submit_button.click()
即使使用您的垃圾凭据,我第一次运行脚本时,它也会按照您描述的那样运行。然而,第二次——它似乎真的做出了反应。这是脚本末尾的状态-如果您有真实的凭据,它看起来会起作用:

更新:

这似乎是你登陆的页面,可能是cookies的设置方式,或者是关于JS的一些东西

作为测试问题边界的另一种方法- 如果您登录主页并接受cookies,则导航到身份验证并登录,它似乎可以工作:


driver = webdriver.Chrome()

# Goto login page - use this to get the cookies
driver.get("https://www.currys.co.uk/gbuk/index.html")
# Close cookie popup if shown
try:
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
except:
    pass

#then proceed to login
driver.get("https://www.currys.co.uk/gbuk/s/authentication.html")

### then the rest!

极好的我很高兴,这只是他们的网站奇怪,而不仅仅是我!这真的很有效。你知道为什么登录cookie(即记住我)看起来没有效果吗?我知道其他cookie被持久化到用户数据中,因为允许cookie的cookie被记住了!