Reputation: 8284
I am trying to call a click function, lastly in my /main.py
file.
/main.py
"""Start Point"""
from data.find_pending_records import FindPendingRecords
from vital.vital_entry import VitalEntry
import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
if __name__ == "__main__":
try:
# for PENDING_RECORDS in FindPendingRecords().get_excel_data(): begin to loop through entire directory
PENDING_RECORDS = FindPendingRecords().get_excel_data()
# Do operations on PENDING_RECORDS
# Reads excel to map data from excel to vital
MAP_DATA = FindPendingRecords().get_mapping_data()
# Configures Driver
VITAL_ENTRY = VitalEntry()
# Start chrome and navigate to vital website
VITAL_ENTRY.instantiate_chrome()
# Begin processing Records
VITAL_ENTRY.process_records(PENDING_RECORDS, MAP_DATA)
# Save Record
VITAL_ENTRY.save_contact(driver)
print (PENDING_RECORDS)
print("All done")
except Exception as exc:
# print(exc)
raise
/vital_entry.py
class VitalEntry:
"""Vital Entry"""
def save_contact (self, driver):
driver.implicitly_wait(15)
driver.find_element_by_css_selector("#publishButton").click()
I am continuously getting this error in Prompt:
Traceback (most recent call last):
File "main.py", line 32, in <module>
VITAL_ENTRY.save_contact(driver)
NameError: name 'driver' is not defined
I do not want to create a new chrome session or window... I've also tried chaining this on my VITAL_ENTRY.process_records(PENDING_RECORDS, MAP_DATA)
above. As you can see I am already importing the driver; and I am using it in the above calls - I don't want to create a new browser instance.
Here is the .instantiate_chrome()
below:
def instantiate_chrome(self):
"""Create Chrome webdriver instance."""
self.options.headless = config.HEADLESS
if not self.options.headless:
self.options.add_argument("--start-maximized")
self.options.add_argument('--disable-infobars')
self.options.add_argument('--disable-gpu')
self.driver = webdriver.Chrome(options=self.options)
self.driver.set_page_load_timeout(30)
self.driver.implicitly_wait(15)
self.driver.get(config.VITAL_URL)
Upvotes: 0
Views: 834
Reputation: 5955
So you create the browser session, then you never pass it out of that function. If you want to use it elsewhere, your instantiate_chrome()
code will need to return driver
, then you'll need to assign it as I stated in my previous comment
driver= VITAL_ENTRY.instantiate_chrome()
Upvotes: 1