如何使用Selenium处理Google Authenticator [英] How to handle Google Authenticator with Selenium

查看:176
本文介绍了如何使用Selenium处理Google Authenticator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Sellercentral.amazon.de下载大量的excel文件(估计:500-1000).手动下载不是一种选择,因为每次下载都需要单击几下,直到弹出Excel.

I need to download a massive amount of excel-files (estimated: 500 - 1000) from sellercentral.amazon.de. Manually downloading is not an option, as every download needs several clicks until the excel pops up.

由于亚马逊无法为我提供具有其结构的简单xml,因此我决定自行进行自动化.首先想到的是Selenium和Firefox.

Since amazon cannot provide me a simple xml with its structure, I decided to automate this on my own. The first thing coming to mind was Selenium and Firefox.

问题:

需要登录到Sellercentral以及2要素验证(2FA).因此,如果我登录一次,则可以打开另一个选项卡,输入Sellercentral.amazon.de并立即登录. 我什至可以打开浏览器的另一个实例,并立即在该实例中登录.他们可能正在使用会话cookie. 抓取"的目标URL为 https://sellercentral.amazon.de/listing/download?ref = ag_dnldinv_apvu_newapvu .

A login to sellercentral is required, as well as 2-factor-authentication (2FA). So if I login once, i can open another tab, enter sellercentral.amazon.de and am instantly logged in. I can even open another instance of the browser, and be instantly logged in there too. They might be using session-cookies. The target URL to "scrape" is https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu .

但是,当我使用selenium webdrive从python-script打开URL时,启动了浏览器的新实例,但我没有登录.我已经登录了.所以我想selenium启动的实例有些不同.

But when I open the URL from my python-script with selenium webdrive, a new instance of the browser is launched, in which I am not logged in. Even though, there are instances of firefox running at the same time, in which I am logged in. So I guess the instances launched by selenium are somewhat different.

我尝试过的事情:

我尝试在第一个.get()之后设置一个时间延迟(以打开网站),然后我将手动登录,然后重做.get(),这将使脚本永久运行.

I tried setting a timedelay after the first .get() (to open site), then I'll manually login, and after that redoing the .get(), which makes the script go on for forever.

from selenium import webdriver
import time


browser = webdriver.Firefox()

# Wait for website to fire onload event
browser.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")

time.sleep(30000)

browser.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")

elements = browser.find_elements_by_tag_name("browse-node-component")


print(str(elements))

我在找什么?

需要使用Google身份验证器中的两因素身份验证令牌的解决方案.

Need solution to use the two factor authentication token from google authenticator.

我希望在firefox浏览器的现有实例中将硒作为选项卡打开,我已经在此处进行了预先登录.因此,无需登录(应该),并且可以进行抓取"和下载. 如果没有直接的方法,也许有人想出一种解决方法?

I want the selenium to be opened up as a tab in the existing instance of the firefox browser, where I will have already logged in beforehand. Therefore no login (should be) required and the "scraping" and downloading can be done. If there's no direct way, maybe someone comes up with a workaround?

我知道硒无法下载文件本身,因为弹出窗口不再是浏览器的一部分.我到达那儿后会解决的.

I know selenium cannot download the files itself, as the popups are no longer part of the browser. I'll fix that when I get there.

重要说明: Firefox不是给定的!我很乐意接受任何浏览器的解决方案.

Important Side-Notes: Firefox is not a given! I'll gladly accept a solution for any browser.

推荐答案

以下是将读取Google身份验证器令牌并在登录中使用的代码.使用js打开新标签页. 在运行测试代码之前,请先安装pyotp软件包.

Here is the code that will read the google authenticator token and used in the login. Used js to open the new tab. Install pyotp package before running the test code.

pip安装pyotp

pip install pyotp

测试代码:

from pyotp import *
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")
wait = WebDriverWait(driver,10)
# enter the email
email = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@name='email']")))
email.send_keys("email goes here")

# enter password
driver.find_element_by_xpath("//input[@name='password']").send_keys("password goes here")

# click on signin button
driver.find_element_by_xpath("//input[@id='signInSubmit']").click()

#wait for the 2FA feild to display
authField = wait.until(EC.presence_of_element_located((By.XPATH, "xpath goes here")))
# get the token from google authenticator
totp = TOTP("secret goes here")
token = totp.now()
print (token)
# enter the token in the UI
authField.send_keys(token)
# click on the button to complete 2FA
driver.find_element_by_xpath("xpath of the button goes here").click()
# now open new tab
driver.execute_script("""window.open("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")""")
# continue with your logic from here

这篇关于如何使用Selenium处理Google Authenticator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆