这是将读取Google身份验证器令牌并在登录中使用的代码。使用js打开新标签页。
pyotp在运行测试代码之前安装软件包。
pip安装pyotp
测试代码:
from pyotp import *from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Bydriver = webdriver.Firefox()driver.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")wait = WebDriverWait(driver,10)# enter the emailemail = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@name='email']")))email.send_keys("email goes here")# enter passworddriver.find_element_by_xpath("//input[@name='password']").send_keys("password goes here")# click on signin buttondriver.find_element_by_xpath("//input[@id='signInSubmit']").click()#wait for the 2FA feild to displayauthField = wait.until(EC.presence_of_element_located((By.XPATH, "xpath goes here")))# get the token from google authenticatortotp = TOTP("secret goes here")token = totp.now()print (token)# enter the token in the UIauthField.send_keys(token)# click on the button to complete 2FAdriver.find_element_by_xpath("xpath of the button goes here").click()# now open new tabdriver.execute_script("""window.open("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")""")# continue with your logic from here


