Python无法从字典发送密钥

时间:2018-08-23 12:50:26

标签: python selenium dictionary selenium-webdriver

我正在尝试使用硒自动化任务。我的代码将分解为我定义的函数。

我首先阅读一张Excel工作表,并将该数据作为字符串存储在字典中。

def get_excel():
    d = defaultdict(list)

    workbook = openpyxl.load_workbook(sys.argv[1])
    sheet = workbook.get_sheet_by_name('Sheet1')

    row_count = sheet.max_row
    for r in range(2, row_count + 1):
        d[str(sheet.cell(r, 4).value)].append((str(sheet.cell(r, 1).value), str(sheet.cell(r, 2).value), str(sheet.cell(r, 3).value)))
    return d

现在我有一个单独的函数,可以从excel获取数据:

def get_emp_data(FirstName, LastName, Email1, EmployeeID, driver):
    f_name = driver.find_element_by_name('f_name')
    l_name = driver.find_element_by_name('l_name')
    email = driver.find_element_by_name('contact_id.email')
    employeeID = driver.find_element_by_name('contact_id.custom')

    time.sleep(5)
    #writing in the fields
    f_name.send_keys(FirstName)
    l_name.send_keys(LastName)
    email.send_keys(Email1)
    employeeID.send_keys(EmployeeID)

    #clicking save button
    save = driver.find_element_by_id('saveButton').click()

    driver.get('link to web address')

接下来,我有一个执行自动化的功能。它会传入Excel工作表中的数据。

def log_in():
    chrome_path = ("C:\\chromedriver.exe")

    driver = webdriver.Chrome(chrome_path)
    driver.get("link to web address")

    #setting variables for username and password fields
    username = driver.find_element_by_name('login')
    password = driver.find_element_by_name('password')

    #input email and password
    username.send_keys("my email")
    password.send_keys("my password")

    #click submit
    driver.find_element_by_id('submitButton').click()

    #click New Customer Button
    time.sleep(5)
    driver.find_element_by_id('newCustomerButton').click()
    return driver

最后,我将所有内容都放入了主函数中

def main():
    excel_data = get_excel()
    driver = log_in()
    time.sleep(2)
    for EmployeeID in excel_data:
        try:
            get_emp_data(excel_data[EmployeeID][0], excel_data[EmployeeID][1], excel_data[EmployeeID][2], EmployeeID, driver)
        continue
    except:
        print(FirstName)

然后我叫main()

main()

除了写excel的值外,代码还可以做所有应做的事情。它应该为log_in(),使用excel中的第一行数据,单击“保存”,转到下一个编辑页面,并使用excel中的下一行数据,然后再次保存,直到excel中没有更多数据为止。我认为将数据传递到函数中的方式存在问题。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我已经解决了所有与该错误有关的问题,并且自动化的代码/过程完美运行!解析的代码如下:

for (( i=0; i < num_volumes; i++ )); do
    CURID=''
    create_volume 'CURID'
    volumes[$i]=${CURID}
      printf "Attach volume ID: %s\\n" "${volumes[i]}"
      if [ \( "$platform" = "Windows" \) ] || [ \( "$platform" = "windows" \) ]; then
        device="/dev/sd${devices[i]}"
        aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" 2>&1 | sed -e 's/An error occurred (InvalidVolume.ZoneMismatch) //g' | jq '.'
        aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume"  Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
      elif [ \( "$platform" = "Linux" \) ]  || [ \( "$platform"  = "linux" \) ]; then
        device="/dev/xvd${devices[i]}"
        aws ec2 attach-volume --volume-id "${volumes[i]}" --instance-id "$tag_instance_id" --device "$device" --profile="$aws_key" 2>&1 | sed -e 's/An error occurred (InvalidVolume.ZoneMismatch) //g' | jq '.'
        aws ec2 create-tags --resources "${volumes[i]}" --tags Key=Name,Value="$name_tag volume"  Key=DeviceName,Value="$device" Key=Application,Value="$application_tag" Key=Engagement,Value="$engagement_tag" Key=Owner,Value="$owner_tag" Key=Role,Value="$role_tag" --profile="$aws_key"
      else
        printf "That is not a valid choice.\\n"
      fi
  return 
  done