AttributeError:实例仅在Linux中没有属性错误

时间:2018-12-26 10:02:37

标签: python python-2.7

我仅在Linux中遇到AttributeError: PortalTesting instance has no attribute 'reporting_screenshot'错误。当我在Windows上运行此代码时,其工作正常。

#!/usr/bin/env python


class PortalTesting:

    def __init__(self):

        self.start_time = time.time()
        self.status = OrderedDict()
        self.screenshots_folder = ''
        self.new_folder_name = ''
        self.tday = datetime.today().strftime('%Y%m%d-%H%M')
        self.dt = date.today() - timedelta(2)
        self.start_date = self.dt.strftime('%m-%d-%Y')

        # self.reporting_screenshot = ''
        # self.transcript = ''


    def login(self):

        try:
            URL = ''
            driver.get(URL)


    # ------------------- Transcript ------------------- #

    def transcript(self):
        try:
            tr_e1 = driver.find_element_by_id('Transcript_ID')
            hover1 = ActionChains(driver).move_to_element(tr_e1)
            hover1.click().perform()
            ....
            ....

            self.transcript_screenshot = self.path + self.tday + "_Transcripts.png"
            driver.get_screenshot_as_file(self.transcript_screenshot)
            self.status['Transcript'] = ["Pass", self.transcript_screenshot]

        # THIS IS WHERE I GET ERROR
        except:
            self.status['Transcript'] = ["Fail", self.transcript_screenshot]

    # ------------------- Reporting ------------------- #

    def reporting(self):

        try:

            # Reports tab
            rpt_e1 = driver.find_element_by_id('report_menu_id')
            hover1 = ActionChains(driver).move_to_element(rpt_e1)
            hover1.click().perform()

            .....
            .....
            WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, 'PortalChartContainer'))).click()
            self.reporting_screenshot = self.tday + "_Reports.png"
            driver.get_screenshot_as_file(self.path + self.reporting_screenshot)
            print("Reporting check complete.")
            self.status['Reporting'] = ["Pass", self.reporting_screenshot]

    # THIS IS WHERE I GET ERROR
        except:
            self.status['Reporting'] = ["Fail", self.reporting_screenshot]


pt = PortalTesting()

pt.login()
pt.reporting()
pt.transcript()
pt.admin()
pt.print_status()

我在这里要做的是登录URL。运行报告/成绩单。如果成功,则将Pass和屏幕截图添加到self.status词典中。如果失败,则将失败和屏幕截图添加到self.status字典中。

上面的代码在Windows上工作正常,但是当我在Linux上复制粘贴相同的代码时出现错误。

如果我在self.transcript = ''中添加self.reporting_screenshot = ''__init__,则会得到TypeError: 'str' object is not callable

回溯错误:

Traceback (most recent call last):
File "DC1PortalTesting.py", line 477, in <module>
 pt.reporting()
 File "DC1PortalTesting.py", line 375, in reporting
 self.status['Reporting'] = ["Fail", self.reporting_screenshot]
 AttributeError: PortalTesting instance has no attribute 'reporting_screenshot'

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

@merlyn是正确的:第一个错误是由于try/except定义中的reporting()语句引起的。如果在之前 self.reporting_screenshot = self.tday + "_Reports.png"之前弹出任何错误,则该属性不存在,并且except子句将引发此错误。

这里有两个建议:

    首先应在self.reporting_screenshot的定义中创建
  1. reporting(),因为此时它的所有组件都已经存在。

    def reporting(self):
    
        self.reporting_screenshot = self.tday + "_Reports.png"    
    
        try:
    
            # Reports tab
            rpt_e1 = driver.find_element_by_id('report_menu_id')
            hover1 = ActionChains(driver).move_to_element(rpt_e1)
            hover1.click().perform()
    
            .....
            .....
            WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, 'PortalChartContainer'))).click()
            # Removed line here
            driver.get_screenshot_as_file(self.path + self.reporting_screenshot)
            print("Reporting check complete.")
            self.status['Reporting'] = ["Pass", self.reporting_screenshot]
    ...
    
  2. 在您的except语句中捕获错误,以进行调试。

    ...
    # THIS IS WHERE I GET ERROR
        except Exception as e:  # catches any exception
            # print(e)  # uncomment for debugging, or make it smarter with a switch
            self.status['Reporting'] = ["Fail", self.reporting_screenshot]
    

关于另一个错误:@merlyn在评论中也提供了解释。

关于为什么代码在Linux上失败的原因:我认为它在Windows上可以工作,所以您从未在此处测试except子句,对吗?