Python 我无法将列表作为自动函数的字符串执行
我有一个学校项目,我的程序必须执行此代码才能跟踪两个用户:Python 我无法将列表作为自动函数的字符串执行,python,string,arraylist,instagram,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
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.")
您需要迭代列表,以便能够在多个值上执行此操作。鉴于这是一个学校项目,我不太愿意给你一个完整的答案 这最好是通过使用循环的
来实现的,您可以在此处阅读有关这些循环的更多信息:
您将经常遇到并使用for
循环,它们可能非常有用,例如:
values = [1,2,3,4,5,6,7,8,9,10]
# This translates to "For each item stored in the list 'values' perform the following code"
for item in values:
print(item)
1
2
3
4
5
6
7
8
9
10
您正在访问列表。也许你想做的是访问它的元素?例如在:usernames[0]中,谢谢,我将对for循环进行更多的研究并重写,这样我就能理解这里正在做的事情。@BotBot欢迎:-),继续努力学习python:d谢谢你给我指明了正确的方向,我将研究for循环,这样我就能真正理解代码中发生的事情。我打算重写以备记忆