Search code examples
pythonpython-3.xautomationgmail

Using Selenium to open Gmail in AWS EC-2


thanks for your support, I'm new into Python. I'm automating a task of:

  1. Opening Gmail
  2. Login and Password
  3. Find E-Mail
  4. Click in an e-mail (It's 8 different ones, each with a specific subject)
  5. Click in a link and download a file
  6. Transforming a .XLSX to .CSV
  7. Upload .CSV to Google Sheet

QUESTIONS / CHALLENGES: I would like to run this script on a daily basis - through a Virtual Machine (AWS EC2 or other), but it seems that using Selenium for the above it only works if I have my Chromium window active. Do you know if I upload the same to EC-2 the same will be applicable or not?

Maybe another alternative would be through Gmail API - but I couldn't achieve the same result listed above.

Here's part of the code which shows the above:

# opening gmail
browser = webdriver.Chrome(executable_path='mydriver.exe', chrome_options=chromeOptions)
browser.get('Gmail URL in HTML')
time.sleep(3)

## e-mail and password
email_field = browser.find_element_by_name('identifier')
email_field.send_keys('[email protected]')
email_field.send_keys(Keys.ENTER)
time.sleep(5)
password_field = browser.find_element_by_name("password")
password_field.send_keys('mypassword')
password_field.send_keys(Keys.ENTER)
time.sleep(5) #Wait for the e-mail screen to open

## finding e-mail 1
finding = browser.find_element_by_xpath('//*[@id="sbq"]')
finding.send_keys('my search query')
finding.send_keys(Keys.ENTER)
time.sleep(3)

## clicking e-mail 1
email = browser.find_element_by_xpath('/html/body/table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[2]/form/table[2]/tbody/tr[1]/td[3]/a/span')
email.click()
time.sleep(3)


## downloading 1
link = browser.find_element_by_xpath('/html/body/table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[2]/table[4]/tbody/tr/td/table[1]/tbody/tr[4]/td/div/div/div/div[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[4]/td/table/tbody/tr[2]/td/a/b')
link.click()
time.sleep(3)

## changing window 1
browser.switch_to.window(browser.window_handles[0])
time.sleep(1)

## going back again
back = browser.find_element_by_xpath('/html/body/table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[2]/table[1]/tbody/tr/td[1]/b/a')
back.click()
time.sleep(2)

Thanks for your time,


Solution

  • If someone ever come across - the solution is to add user-agent in the ChromeOptions. Once we're running headless this will solve the problem.

    Here's the quick code:

    ChromeOptions.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36')