简体   繁体   中英

Environment Variables - Selenium - Python

I am attempting to create a web crawler utilizing Selenium via python. I have to login into a website and attempted to use two environment variables instead of hard coding my credentials into the script. However when I attempt to do so I get an error like:

Traceback (most recent call last):
  File "/Users/KAR/Desktop/Web Scraping /Clicky2.py", line 28, in <module>
    username_box.send_keys(USERNAME)
  File "/Users/KAR/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "/Users/KAR/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/common/utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()

As for my code: the code provided works when the credentials are hard coded but, I would prefer to have environment variables.

To be as specific as possible - the issue is that when I use environment variables to attempt to login using Selenium I receive an error as seen above.

If someone could help me with this, it would be greatly appreciated!

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
import requests
import os

#path to chromedriver
DRIVER_PATH = '/usr/local/bin/chromedriver'
#environment variables credentials for Clicky
#USERNAME = os.getenv('USERNAME')
USERNAME = 'user'
#PASSWORD = os.environ.get('PASSWORD')
PASSWORD = 'password'

#allows me to put Selenium in headless mode or without running GUI
options = Options()
options.headless = True

#opens Chrome, goes to Clicky login
driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH)
driver.maximize_window()
driver.implicitly_wait(20)
driver.get('https://clicky.com/user/login')
print(driver.current_url)
print('Clicky is opened')
sleep(1)

username_box = driver.find_element_by_name("username")
username_box.clear()
username_box.send_keys(USERNAME)
sleep(1)

password_box = driver.find_element_by_name("password")
password_box.clear()
password_box.send_keys(PASSWORD)
sleep(1)

login_button = driver.find_element_by_name("submit_button")
login_button.click()

print(driver.current_url)
driver.quit()

You can set your environment variables in a.env file and use the pytest plugin pytest-dotenv.

https://pypi.org/project/pytest-dotenv/

As an aside, you can also install your browser drivers so they are on your local machine so that eliminates the need to define the path to the driver in your script. Example: brew install --cask chromedriver . If you are running these in a CI system there are other solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM