2016-07-26 36 views
0

變量擴展不會在多個引號內發生。下面是代碼python變量擴展在多引號中使用appium查找元素

products = ['One','Two','Three'] 
for i in range(0,len(products)): 
    el = self.driver.find_element_by_android_uiautomator('new UiSelector().text("{}")').format(products[i]) 
       print (el) 

預期的結果應該是:

el = self.driver.find_element_by_android_uiautomator('new UiSelector().text("One")') 

錯誤消息:

File "temp_test.py", line 84, in test_product 
    el = self.driver.find_element_by_android_uiautomator('new UiSelector().text("{}")').format(products[i]) 
NoSuchElementException: Message: An element could not be located on the page using the given search parameters. 

請幫我解決這個問題!

回答

0

最後,多次嘗試(幾乎半天)得到答案。它的簡單,.format(products[i])應該在括號()和引號結尾處指定。

products = ['One','Two','Three'] 
for i in range(0,len(products)): 
    el = self.driver.find_element_by_android_uiautomator('new UiSelector().text("{}")'.format(products[i])) 
       print (el)