【问题标题】:what is a better option than manually index in Python?有什么比在 Python 中手动索引更好的选择?
【发布时间】:2019-11-08 21:45:54
【问题描述】:

我是 python 的初学者。目前,我正在使用嵌套列表中的数据自动填充网页字段。我能够做到这一点,如下面的代码所示:-

webpage_fields = [['a','b','c'],['x','y','z']]

for i in range(len(webpage_fields):

    driver.find_element_by_xpath(add_field).click()
    driver.find_element_by_xpath(field_name_box).send_keys(webpage_fields[i][0], Keys.TAB,
                                                        webpage_fields[i][1], Keys.ENTER, 
                                                        webpage_fields[i][2], Keys.ENTER)
   driver.find_element_by_xpath(save_page).click()

这只是一个例子。但是在处理更长的嵌套列表时,有什么比手动写下项目索引更好的选择呢?谢谢

【问题讨论】:

  • 这并没有很好的简化,因为您需要在每个元素之间插入其他参数,例如Keys.TAB
  • 您可以使用for field in webpage_fields: 来简化webpage_fields[i],而不是遍历索引。

标签: python loops for-loop iteration sublist


【解决方案1】:

数据必须以一种或另一种方式发送,因此必须以一种或另一种方式输入。

也许(但它比其他任何东西都更具风格),您会发现将所有内容都放在列表中会更容易阅读,例如:

fillers = [
              ['a', Keys.TAB, 'b', Keys.ENTER, 'c', Keys.ENTER],
              ['x', Keys.TAB, 'y', Keys.ENTER, 'z', Keys.ENTER]
          ]

for filler in fillers:
    driver.find_element_by_xpath(add_field).click()
    driver.find_element_by_xpath(field_name_box).send_keys(*filler)
    driver.find_element_by_xpath(save_page).click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多