我在将列表作为自动化函数的字符串执行时遇到问题
I Am Having Trouble Executing a list as a string for an automated function
我有一个学校项目,我的程序必须执行此代码才能关注两个用户:
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 loops
来实现,您可以在此处阅读更多相关信息:Python FOR Loops
您会经常遇到和使用 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
我有一个学校项目,我的程序必须执行此代码才能关注两个用户:
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 loops
来实现,您可以在此处阅读更多相关信息:Python FOR Loops
您会经常遇到和使用 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