有什么方法可以在元素前面添加数字

时间:2020-03-17 12:38:56

标签: python selenium

我的密码

    sidebar = self.driver.find_element_by_class_name('menusubnav')
    elementList = sidebar.find_elements_by_tag_name("li")
    for i in range(len(elementList)):
       element = self.driver.find_element_by_class_name('menusubnav').find_elements_by_tag_name("li")[i]
       print(enumerate, element.text)

==================================结果============ =============

   C:\DEV\practice\venv\Scripts\python.exe C:/DEV/practice/test_case.py
   test_guru (__main__.LoginGuruTest) ... 
   <class 'enumerate'> Manager
   <class 'enumerate'> New Customer
   <class 'enumerate'> Edit Customer
   <class 'enumerate'> Delete Customer
   <class 'enumerate'> New Account
   <class 'enumerate'> Edit Account
   <class 'enumerate'> Delete Account
   <class 'enumerate'> Deposit
   <class 'enumerate'> Withdrawal
   <class 'enumerate'> Fund Transfer
   <class 'enumerate'> Change Password
   <class 'enumerate'> Balance Enquiry
   <class 'enumerate'> Mini Statement
   <class 'enumerate'> Customised Statement
   <class 'enumerate'> Log out
   ===  Alert shows following message:  ====
   ===========================================
   ||   You Have Succesfully Logged Out!!   ||
   ===========================================
   ok
   test_screenshot_logout (__main__.LoginGuruTest) ... ok

   ----------------------------------------------------------------------
   Ran 2 tests in 28.417s

   OK

   Process finished with exit code 0

================================================ ========================

我想要这个----对页面元素的结果进行编号

================================================ ======================

     1. Manager
     2. New Customer
     3. Edit Customer
     4. Delete Customer
     5. New Account
     6. Edit Account
     7. Delete Account
     8. Deposit
     9. Withdrawal
     10. Fund Transfer
     11. Change Password
     12. Balance Enquiry
     13. Mini Statement
     14. Customised Statement
     15. Log out

================================================ =========================

谢谢

2 个答案:

答案 0 :(得分:0)

这足够了:

for i, element in enumerate(elementList):
       print(i+1, element.text)

答案 1 :(得分:0)

亲爱的,请尝试打印此代码,该代码将正常工作

print(str(i + 1) +'.' +element.text)