【问题标题】:TypeError: must be str, not int in python 3TypeError:必须是 str,而不是 python 3 中的 int
【发布时间】:2017-07-03 11:49:45
【问题描述】:

我正在尝试以网站形式提交一些电子邮件。我在 python 3 中使用 selenium。

这是我的代码:

import os
import time
import getpass
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

while True:
    chromedriver  = 'F:\All  Folders\chromedriver\chrome.exe'

#Uncomment this block if you don't want images to load(makes the procss a little bit faster)
    '''
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"profile.managed_default_content_settings.images":2}
    chromeOptions.add_experimental_option("prefs",prefs)
    browser = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
    '''

    browser = webdriver.Chrome(chromedriver)
    browser.get("http://www.website.com")       # website's home page
    time.sleep(10)

    # Logging into website
    form = browser.find_element_by_class_name('regular_login')
    email = form.find_element_by_name("email")
    password = form.find_element_by_name("password")
    button_element = browser.find_element_by_xpath("//*[@value='Login']")

    #List of emails
    email_list = ['na23b9@gmail.com', '25g65b@gmail.si', 'gfdebfk@gmail.jp']
    for emails, emails in enumerate(email_list):
      email.send_keys(emails)
      emails = emails + 1
      print("success")

我的计划是使用 while 循环在每个浏览器会话中提交每封电子邮件。但是这行 'emails = emails + 1' 返回错误。

这是引用:

 F:\Python_Installer\python.exe C:/Users/user/PycharmProjects/Quora_Bot/westing.py

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 39, in <module>
    emails = emails + 1
TypeError: must be str, not int

Process finished with exit code 1

我不知道。谁能给我解释一下。

【问题讨论】:

    标签: python-3.x selenium selenium-chromedriver


    【解决方案1】:

    我相信问题出在

    for emails, emails in enumerate(email_list):
      emails = emails + 1
    

    首先,但这不是导致错误的原因,要么使用for i, emails in enumerate(email_list) 并执行i 计数器的操作,或者如果您只需要列表的内容,则只需使用for emails in email_list。

    其次,删除 emails = emails + 1 :在此步骤中,您尝试将 1(整数)添加到 emails(字符串),这会导致错误,并且似乎没有任何用途,因为循环的下一步是为 emails 重新分配一个值,因为它是您的迭代器。

    所以,如果你想做的是遍历你的电子邮件列表以便一个接一个地发送它们,那么就这样做:

    for email_adress in email_list:
        email.send_keys(email_adress)
    

    我希望这会有所帮助。

    后记:

    照原样,您的代码将运行一个无限循环,这是您不想要的。在某些情况下,在使用break 语句时运行无限循环非常有用,但您似乎没有这种情况;此外,由于您似乎正在尝试迭代会话,因此您只需要嵌套 for 循环。

    【讨论】:

    • 首先非常感谢@Archeo 的详细回答。但for email_adress in enumerate(email_list): email.send_keys(email_adress) 不起作用并发送Traceback (most recent call last): File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 30, in &lt;module&gt; email.send_keys(email_adress) File "C:\Users\user\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webelement.py", line 348, in send_keys {'text': "".join(keys_to_typing(value)), TypeError: sequence item 0: expected str instance, int found
    • 然后我尝试了:for i, emails in enumerate(email_list): email.send_keys(email_adress)
    • 在第一种情况下,这是我的错:我打错了答案(现已更正)。当您要遍历列表的内容时,不应使用 enumerate 函数(请阅读文档以了解原因):for email_adress in email_list: email.send_keys(email_adress)。
    • 在第二种情况下,这是因为您指的是不存在的email_adress 变量:如果您想使用计数迭代器i,您可以使用for i, email_adress in enumerate(email_list): email.send(email_adress)。请理解你的变量是什么,否则你的代码只能是乱七八糟的、破碎的、难以阅读的(无意冒犯,这只是一个事实,不是判断或攻击你的编码能力)。
    • 非常感谢@Archeo 抽出宝贵时间。但不幸的是,他们两个都没有工作。因为他们将所有电子邮件提交到表单中。但我发现了另一种使用文本文件的方法。我将创建一篇关于它的博客文章,并将从这里链接。再次,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2018-07-23
    • 1970-01-01
    • 2018-04-26
    • 2019-04-23
    相关资源
    最近更新 更多