-2

I am automating a process of importing data into a CSV from my website. The traceback can be seen below. I am getting a NameError stating that mobile and office are not defined within the if/elif conditions.

EDIT: Assume a blank Book1.csv

from selenium import webdriver
import time
import csv
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("undisclosable")

driver.find_element_by_xpath("//input[@name='rad_region' and @value='far']").click()
time.sleep(2)
driver.find_element_by_xpath("//input[@name='rad_georegion' and @value='east']").click()
time.sleep(2)
driver.find_element_by_xpath("//input[@name='rad_manage' and @value='No']").click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="ModalPopupDiv"]/div/div[3]/div[1]/a').click()

class Search():
    with open(r'Book1.csv', 'r+', encoding='utf-8') as csvfile:
        csvreader = csv.reader(csvfile)
        next(csvreader)
        while True:
                for row in csvreader:
                    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "searchKeyword")))
                    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "go")))
                    driver.find_element_by_id('searchKeyword').clear()
                    time.sleep(1)
                    driver.find_element_by_id('searchKeyword').send_keys(row)
                    time.sleep(1)
                    driver.find_element_by_id('go').click()
                    time.sleep(1)

                    writer = csv.writer(csvfile)
                    tobesplit = str(driver.find_element_by_class_name('snippetContact').text)
                    emailandphone = tobesplit.split("  |  ")
                    try:
                        email = emailandphone[0]
                        office = emailandphone[1]
                        mobile = emailandphone[2]
                        writer.writerow([None, email, office, mobile])
                    except UnboundLocalError:
                        if mobile not in emailandphone:
                            writer.writerow([None, email, office])
                        elif office not in emailandphone:
                            writer.writerow([None, email])

Here is the stacktrace:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 19, in <module>
    class Search():
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 43, in Search
    if mobile not in emailandphone:
NameError: name 'mobile' is not defined

Process finished with exit code 1
  • Hi! Welcome to Stack Exchange. Could you rephrase the question to clarify which could raises which error? Your code is a bit unclear. `str` is not defined in it, and it is not syntactically correct. You might want to consult on what is required of [MCVE](https://stackoverflow.com/help/mcve) – tmrlvi Nov 22 '17 at 01:07
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 22 '17 at 01:08
  • My apologies. I have viewed the MCVE and have updated some of my code and will continue to do so. – Tanner Douglas Nov 22 '17 at 01:21
  • I'm very sorry if my code or my writing did not describe my issue. This is exactly what my code looks like as an example. Please feel free to give me advice, as I am new to the process. Thank you! – Tanner Douglas Nov 22 '17 at 01:33
  • Just to clarify what others are saying: It would be help to (1) have the exact error that you see, including stack trace, and (2) some code that I could copy/paste onto my system, and that would run and give the same error. Maybe (2) is not as easy, since we don't have your "Book1.csv" file. – jwd Nov 22 '17 at 01:52
  • Hi, thanks for the clarification. The error for parameters within both conditional's writerows are: Name 'example2' can be not defined This inspection warns about local variables referenced before assignment. – Tanner Douglas Nov 22 '17 at 02:09

1 Answers1

0

1> If the data you are parsing does not have enough elements separated by delimiters ,then you will get the below error and it will move to except block , this means the Mobile variable assignment never happens, so the except block raises an error. I hope this is clear. Also attaching an example for clarity where the error is raised even earlier and they suggest using else at the end of try except for a default value ("none" probably for your use case)

Traceback (most recent call last):
  File "C:/Users/s995648.CS/PycharmProjects/untitled/auto", line 39, in Search
    office = emailandphone[1]
IndexError: list index out of range

2> Its good practise to check if your lists are populating as you assume before trying to access value, simplest way being checking length of each element after assignment, and handle accordingly

kmcodes
  • 807
  • 1
  • 8
  • 20