Home > database >  Selenium Chrome Driver Error: TypeError: object of type 'NoneType' has no len()
Selenium Chrome Driver Error: TypeError: object of type 'NoneType' has no len()

Time:05-27

I'm testing a financial application through the use of selenium, and I have already fixed the driver specification to what my browser uses in my operating system. But now there is a NoneType Error with the send_keys(os.environget([Variable]). Here is my code.....

# plotting Trends
import re, os
from dotenv import load_dotenv
import numpy as np
from numpy import inf
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup as bs
import pandas as pd
import time
import seaborn as sns
sns.set() # setting seaborn default for plots
from pdb import set_trace


load_dotenv()

dr = webdriver.Chrome("/home/name/Desktop/chromedriver") #chrome_options=chrome_options)
dr.get("https://www.screener.in/login/")

username = dr.find_element_by_id("id_username")
username.clear()
SCRUSER = "[email protected]"
username.send_keys(os.environ.get(SCRUSER))

password = dr.find_element_by_id("id_password")
password.clear()
SCRPASSWORD = "password"
password.send_keys(os.environ.get(SCRPASSWORD)) 

.....

Here is my error:

  /home/robert/Desktop/diagram1.py:22: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  dr = webdriver.Chrome("/home/robert/Desktop/chromedriver") #chrome_options=chrome_options)
Traceback (most recent call last):
  File "/home/robert/Desktop/diagram1.py", line 28, in <module>
    username.send_keys(os.environ.get(SCRUSER))
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webelement.py", line 511, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "/usr/lib/python3/dist-packages/selenium/webdriver/common/utils.py", line 153, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()

CodePudding user response:

SCRUSER = "[email protected]"
username.send_keys(os.environ.get(SCRUSER))

It appears that you are assigning the username and password a value, but then trying to call its environment variable. Instead of looking at your .env file for the value of "SCRUSER", it is looking for the value of "[email protected]"

Try:

SCRUSER = os.environ.get("SCRUSER")
SCRPASSWORD = os.environ.get("SCRPASSWORD")

Then, when you send the keys, just send the variable itself

Or, you can just add quotes and not assign it to a variable.

username.send_keys(os.environ.get("SCRUSER"))
password.send_keys(os.environ.get("SCRPASSWORD"))

CodePudding user response:

I think the environment variables SCRUSER and SCRPASSWORD hasn't been loaded in your script. One of the possible approach is to store your environment in an env file in the following format

creds.env

SCRUSER="[email protected]"
SCRPASSWORD="password"

and then in the python code, you can load it using the command load_dotenv(<path to env file>)

import os
from dotenv import load_dotenv

load_dotenv("creds.env")
print(os.environ.get("SCRUSER"))
print(os.environ.get("SCRPASSWORD"))

After the env file is loaded, you can access to the value using

scuser = os.environ.get("SCRUSER")
scpassword = os.environ.get("SCRPASSWORD")
  • Related