【问题标题】:undefined variable 'driver' / don't create new chrome instance未定义的变量“驱动程序”/不要创建新的 chrome 实例
【发布时间】:2019-08-10 09:55:57
【问题描述】:

我正在尝试调用点击函数,最后在我的/main.py 文件中。

/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() 

我在提示中不断收到此错误:

Traceback (most recent call last):
  File "main.py", line 32, in <module>
    VITAL_ENTRY.save_contact(driver)
NameError: name 'driver' is not defined

我不想创建新的 chrome 会话或窗口...我也尝试在上面的 VITAL_ENTRY.process_records(PENDING_RECORDS, MAP_DATA) 上链接它。如您所见,我已经在导入驱动程序;我在上述调用中使用它 - 我不想创建新的浏览器实例。

下面是.instantiate_chrome()

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)

【问题讨论】:

  • 是的,您还没有在范围内的任何地方定义driver。您可能想使用实例变量
  • 如何在此处添加范围?我不想启动一个新的单独的浏览器实例/会话。
  • 你昨天也问过同样的问题,我会补充我之前评论的内容。您在此代码中有一行:# Configures Driver VITAL_ENTRY = VitalEntry(),虽然我们看不到该函数的代码,但我假设将驱动程序创建为 VITAL_ENTRY,而不是 driver
  • 或者,再次没有足够的代码来查看问题所在,也许您需要修改 VITAL_ENTRY.instantiate_chrome() 以返回一个 chromedriver 对象以用作您的 chrome 驱动程序
  • 感谢 @G.Anderson 我刚刚添加了该 instantiate_chrome 代码

标签: python selenium driver


【解决方案1】:

所以你创建了浏览器会话,然后你永远不会将它传递出那个函数。如果您想在其他地方使用它,您的instantiate_chrome() 代码需要return driver,然后您需要按照我之前的评论中所述进行分配

driver= VITAL_ENTRY.instantiate_chrome()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多