-4

I'm trying to iterate two text files. I have two text files one is for 'username' and another is for 'password'.

It works when I use functions with single argument. here's the code but not when it comes to use 2 or more than 1 argument.

Here's my code:

import os
import time
import getpass
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


def Quora_bot():

    username = None
    password = None

    chromedriver  = 'F:\All  Folders\chromedriver\shit.exe'

#Uncomment this block if you don't want images to load(makes the procss a little bit faster)
    '''
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"profile.managed_default_content_settings.images":2}
    chromeOptions.add_experimental_option("prefs",prefs)
    browser = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
    '''

    browser = webdriver.Chrome(chromedriver)
    browser.get("http://www.quora.com")       # Quora home page
    time.sleep(10)

    # Logging into Quora
    form = browser.find_element_by_class_name('regular_login')
    email = form.find_element_by_name("email")
    password = form.find_element_by_name("password")
    button_element = browser.find_element_by_xpath("//*[@value='Login']")
    email.send_keys(username)
    password.send_keys(password)


with open('newfile.txt') as u:
      for line in u:
        rr = line


with open('password.txt') as p:
     for linee in p:
       pp = linee

Quora_bot()
username = rr
password = pp

And I'm getting this error.

    F:\Python_Installer\python.exe C:/Users/user/PycharmProjects/Quora_Bot/westing.py
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 45, in <module>
    Quora_bot()
  File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 32, in Quora_bot
    email.send_keys(username)
  File "F:\Python_Installer\lib\site-packages\selenium\webdriver\remote\webelement.py", line 348, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "F:\Python_Installer\lib\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()

Process finished with exit code 1
12
  • 2
    You haven't included the code that has the error. Edit your question and include the relevant parts. Jul 9, 2017 at 12:11
  • okay I will post full code Jul 9, 2017 at 12:12
  • don't override the Python keywords and builtins like for, object
    – PRMoureu
    Jul 9, 2017 at 12:13
  • okay I will try Jul 9, 2017 at 12:14
  • All I want is iterate two files Jul 9, 2017 at 12:14

2 Answers 2

0

So that we do not end up having everything in the comments let me summarize for you

  1. Don't overwrite inbuild keyword such as for or object

  2. your for_game()' function which is (i guess) should resetletterandanimal` is not resetting the global variables but only locally. So it is pointless since it does nothing in the end.

  3. Assuming you defined valsomewhere where we can't see, make sure it is defined as an object which as a length attribute like a string or list ( i guess most/ all iterables have. At the moment it seems like it is None so either you defined it as Noneor maybe set it equal to a function wich has no return statement.

  4. I dont think your with statements are working the way you want them either, since you never read the content of the file but only keep the object. you need to read it with content = file.read()

But again without the necessary code, we can not help you anymore.

1
  • About ur 4th point, even if he doesnt read the file using read or readlines this will work. If he wants to just print the contents of the file, this should be fine. But if he wants to store it, then it will always store the last line. In that case,as you said he has to use read. Just wanted to add this clarification. Jul 9, 2017 at 12:36
0

The problem is that you send None-Type object to the input. At first you called Quora_bot() and defined there:
username = None
password = None

Then you try to send them as keys to the inputs:
email.send_keys(username)
password.send_keys(password)

And only then you define them:

Quora_bot()
username = rr
password = pp

So it gives you this error:

  for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()

I had the same problem because I didn`t define the path to my .env file and my env-variables had None-Type:

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebElement
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
import dotenv
import os
import time

dotenv.load_dotenv()

MY_EMAIL = os.getenv('my-email')
MY_PASSWORD = os.getenv('my-password')
LINK = 'https://www.linkedin.com/jobs/search/?f_LF=f_AL&geoId=102257491&keywords=python%20developer&location=London%2C%20England%2C%20United%20Kingdom&redirect=false&position=1&pageNum=0'
service = Service('C://Development/chromedriver.exe')
driver = webdriver.Chrome(service=service)

driver.get(LINK)
sigh_in: WebElement = driver.find_element(By.CLASS_NAME, 'nav__button-secondary')
sigh_in.click()
time.sleep(5)
username_input: WebElement = driver.find_element(By.ID, 'username')
username_input.click()

username_input.send_keys(MY_EMAIL)
password_input: WebElement = driver.find_element(By.ID, 'password')
password_input.send_keys(MY_PASSWORD)
sigh_in: WebElement = driver.find_element(By.CLASS_NAME, 'btn__primary--large')
sigh_in.click()

Output:

  Traceback (most recent call last):
  File "C:/Users/T3600/Desktop/PycharmProjects/job-applying/main.py", line 25, in <module>
    username_input.send_keys(MY_EMAIL)
  File "C:\Users\T3600\Desktop\PycharmProjects\job-applying\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 541, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "C:\Users\T3600\Desktop\PycharmProjects\job-applying\venv\lib\site-packages\selenium\webdriver\common\utils.py", line 148, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.