Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Google Authenticator with Selenium

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.

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.

The Problem:

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 .

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.

What I've tried:

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))

What am I looking for?

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

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.

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

like image 235
Lino Avatar asked Apr 26 '19 15:04

Lino


People also ask

How do I automate Google Authenticator using selenium?

Automate Two-factor authentication (2FA) using Selenium You can implement the program that computes the Authenticator code inside your test script easily. Note: Example used in this article is written in JAVA-Gradle project! Step 2 : Copy the external library code based on your project and paste it in your build file.

How do I automate Authenticator app?

Configuring google account 2FA settingsSign into your Google Account and navigate to Security tab. In the security page, click on the option “2-steps verification”, that will take you to the page where you can set up your preferable 2FA options. Find the Authenticator app option and click on set up.

Can we automate 2 factor authentication?

How Two-Factor Authentication Can Be Automated. 2FA comes in many forms, but the two most common are using a mobile app or an authenticator hardware device. Mobile apps, such as Google Authenticator or Authy, generate codes that you need to provide in addition to your username and password.


1 Answers

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 install pyotp

Test code:

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
like image 142
supputuri Avatar answered Nov 15 '22 03:11

supputuri