我制作了一个instabot,每次它使用一个用户名和密码登录并关闭浏览器,然后重新启动并登录另一个用户名或密码,然后使用另一个帐户并重复多次,但我不知道在我的代码中在哪里使用(for(或(while(>
**usenm.send_keys(self.username)**
#import from selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
#my class
class INntagramBot:
def __init__(self,username,password):
self.username=username
self.password=password
self.driver=webdriver.Chrome()
#THIS FUNCTION IS FOR CLOSE browser
def webbrosercloser(self):
self.driver.close()
#THIS FUNCTION IS FOR LOGIN
def login(self):
driver=self.driver
driver.get('https://www.instagram.com/')
usenm=driver.find_element_by_xpath("//input[@name='username']")
usenm.clear()
`#HERE I USE THE USERNAME I THINK HERE I USE FOR `
**usenm.send_keys(self.username)**
passw=driver.find_element_by_xpath("//input[@name='password']")
passw.clear()
passw.send_keys(self.password)
passw.send_keys(Keys.ENTER)
time.sleep(4)
driver.get("https://www.instagram.com/p/B8zjvEAhux8/")
time.sleep(2)
driver.find_element_by_class_name('_8-yf5 ').click()
#username and password
username=['bazamcartoon','taghaviali34']
password="0909"
bb=INntagramBot(username,password)
bb.login()```
试试下面的方法:
#import from selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
#my class
class INntagramBot:
def __init__(self,username,password):
self.username=username
self.password=password
self.driver=webdriver.Chrome()
#THIS FUNCTION IS FOR CLOSE browser
def webbrosercloser(self):
self.driver.close()
#THIS FUNCTION IS FOR LOGIN
def login(self):
driver=self.driver
driver.get('https://www.instagram.com/')
usenm=driver.find_element_by_xpath("//input[@name='username']")
usenm.clear()
`#HERE I USE THE USERNAME I THINK HERE I USE FOR `
**usenm.send_keys(self.username)**
passw=driver.find_element_by_xpath("//input[@name='password']")
passw.clear()
passw.send_keys(self.password)
passw.send_keys(Keys.ENTER)
time.sleep(4)
driver.get("https://www.instagram.com/p/B8zjvEAhux8/")
time.sleep(2)
driver.find_element_by_class_name('_8-yf5 ').click()
#username and password
username=['bazamcartoon','taghaviali34']
password="0909"
for i in range(0, n):-----------> n is the number of times you want to call the function.
bb=INntagramBot(username,password)
bb.login()`
如果你想用while循环运行它,你可以这样做:
i = 0
while i < n: -------------------> n is same as above, the number of times you want to iterate
bb=INntagramBot(username,password)
bb.login()
i+=1