2017-03-06 51 views
1

我運行下面的代碼來驗證網頁上的文字:不斷收到元素沒有連接到網頁文件

def verifyText(self, Text): 
    try: 
     self.switchToFrame(*MainPageLocatars.FRAMEONE) 
     self.switchToFrame(*MainPageLocatars.SUBLISTFRAME) 
    except: 
     pass 
    self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 

我試過(2)每個步驟之後加入time.sleep但它仍然是給我一個錯誤,當我運行這個功能 - >>不斷得到元件沒有以這行代碼

self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 

我在這裏調用該函數,我應該在哪裏重新定義它附加到頁面文件錯誤?

listview = ListView(self.driver, 'First') 
listview.verifyText("comp1") 

注意,行父:

self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 

這是我如何定義函數:

class ListView(Page): 

    def __init__(self, driver, index): 

     if index not in INDEX_MAP: 
      raise ValueError("Invalid index %s" % index) 

     self.driver = driver 

     try: 
      self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 
     # used for VerifyText function only 
     except: 
      self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index]) 


    def verifyText(self, Text): 
     try: 
      self.switchToFrame(*MainPageLocatars.FRAMEONE) 
      self.switchToFrame(*MainPageLocatars.SUBLISTFRAME) 
     except: 
      pass 
     self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 

這裏是全碼:

# all locaters for this class are defined here only 
class ListView(Page): 

    def __init__(self, driver, index): 

     if index not in INDEX_MAP: 
      raise ValueError("Invalid index %s" % index) 

     self.driver = driver 

     try: 
      self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 
     # used for VerifyText function only 
     except: 
      self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index]) 

    @property  
    def row(self): 
     return self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 


    def verifyText(self, Text): 
     try: 
      self.switchToFrame(*MainPageLocatars.FRAMEONE) 
      self.switchToFrame(*MainPageLocatars.SUBLISTFRAME) 
     except: 
      pass 
     self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 

這是現在給我這個錯誤:

Traceback (most recent call last): 
    File "autoLoaderTest.py", line 56, in test02_AutoLoaderSubCompany 
    listview = ListView(self.driver, 'First') 
    File "C:\Users\cverma\Desktop\SOAPProject\mainPage.py", line 44, in __init__ 
    self.row = self.driver.find_element_by_xpath(ListViewLocatars.TEXTPARENT % INDEX_MAP[index]) 
AttributeError: can't set attribute 
+1

這可能是因爲頁面是因爲時間更新您已經聲明'row'元素,所以你不能再使用它了,但需要重新定義它 – Andersson

+0

@Andersson,我需要在哪裏重新定義它?請參閱我的更新問題 – user7242550

+1

顯示什麼是「行」,您如何定義它 – Andersson

回答

1

這是StaleElementReferenceException如何被觸發以及如何避免它的簡化版本。

我想你的代碼就像下面:

self.row = self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 
# Now your self.row is accessible. You can use it 
self.driver.find_element_by_xpath('//input[@type="text"]').send_keys('some data') 
# This action could trigger page refresh 
# Now your self.row is not accessible as it was found on previous page 
self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 
# Here comes StaleElementReferenceException 

如何能避免的話:

@property 
def row(self): 
    return self.driver.find_element_by_xpath(ListViewLocatars.ROWPARENT % INDEX_MAP[index]) 

def verifyText(self, Text): 
    try: 
     self.switchToFrame(*MainPageLocatars.FRAMEONE) 
     self.switchToFrame(*MainPageLocatars.SUBLISTFRAME) 
    except: 
     pass 
    self.row.find_element_by_xpath(ListViewLocatars.VERIFYTEXT % Text) 
    # only now self.row is defined and even if page refreshed 
    # next time you call self.row, element will be re-defined 
+0

時都會重新定義元素,我應該在__init__上將其定義在頂部?我試圖做到這一點,但它給我一個錯誤 類型錯誤:「財產」對象不是可調用 – user7242550

+0

這可能是因爲您嘗試使用它作爲方法 - 'self.row()',而你應該只喜歡使用屬性' self.row',沒有'()'。你不應該在'__init __()'裏面定義它' – Andersson

+0

這就是我定義它的方式。我已經更新了我的問題。 – user7242550

相關問題