python - 我在将列表作为自动化函数的字符串执行时遇到问题

标签 python string arraylist instagram

我有一个学校项目,我的程序必须执行此代码才能关注两个用户:

def followWithUsername(self):
    usernames = ['therock', 'justinbieber']
    self.driver.get('https://instagram.com/' + str(usernames) + '/')
    time.sleep(3)
    followButton = self.driver.find_element_by_css_selector('button')
    if (followButton.text != 'Following'):
        followButton.click()
        time.sleep(3)
    else:
        print("You are already following this user.")

我尝试了各种方法,但这不会将用户名彼此分开并一一执行

最佳答案

使用for循环:

def followWithUsername(self):
    usernames = ['therock', 'justinbieber']
    for name in usernames:
        self.driver.get('https://instagram.com/' + name + '/')
        time.sleep(3)
        followButton = self.driver.find_element_by_css_selector('button')
        if (followButton.text != 'Following'):
            followButton.click()
            time.sleep(3)
        else:
            print("You are already following this user.")

关于python - 我在将列表作为自动化函数的字符串执行时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350206/

相关文章:

java - Java中基于ArrayList的二叉树实现

c# - 通过遍历列表访问每个对象的公共(public)方法

python - python中奇怪的作用域行为

python - 为什么 "Building wheel for TA-Lib (setup.py)"需要很长时间?

python - 如何使用带有 gzip 压缩选项的 pandas read_csv 读取 tar.gz 文件?

Java - 从文件中的特定索引中提取相关字符串/字符

C++ 字符串 - 超出范围错误

python - 为什么 jinja2 过滤 "tojson"排序键?

java - 如果字符串上的语句不起作用

java - 参数在方法中间消失了?