如何为以下代码生成循环。。?

2024-04-20 10:06:43 发布

您现在位置:Python中文网/ 问答频道 /正文


from selenium.webdriver.common.keys import Keys

from time import sleep

driver = webdriver.Chrome()

driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")

sleep (1)

username = driver.find_element_by_name('username')

sleep (1)

username.send_keys('your_username')

sleep (1)

password = driver.find_element_by_name('password')

sleep (1)

password.send_keys('your_password')

sleep (1)

password.send_keys(Keys.RETURN)

sleep (3)

driver.get('https://www.instagram.com/instagram/')

sleep(2)

从头到尾。。。如何使一个循环,将重复这整个部分。。。??你知道吗

follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')

follow_button.click()

sleep (2.5)

unfollow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')

unfollow_button.click()

sleep (2.5)

unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')

unnfollow_button.click()

sleep (2.5)

follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')

follow_button.click()

sleep (2.5)

unfollow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')

unfollow_button.click()

sleep (2.5)

unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')

unnfollow_button.click()

sleep (2.5)

follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')

follow_button.click()

sleep (2.5)

unfollow_button =driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')

unfollow_button.click()

sleep (2.5)

unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')

unnfollow_button.click()

sleep (2.5)

Tags: namebydriverbuttonsleepelementfindclass
1条回答
网友
1楼 · 发布于 2024-04-20 10:06:43

使用while循环?不幸的是,您没有给出关于您的用例的很多细节。你知道吗

def thing_you_want_looped():
    follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')
    follow_button.click()
    sleep(2.5)
    unfollow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')
    unfollow_button.click()
    sleep(2.5)
    unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')
    unnfollow_button.click()
    sleep(2.5)
    follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')
    follow_button.click()
    sleep(2.5)
    unfollow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')
    unfollow_button.click()
    sleep(2.5)
    unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')
    unnfollow_button.click()
    sleep(2.5)
    follow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy.y3zKF')
    follow_button.click()
    sleep(2.5)
    unfollow_button = driver.find_element_by_class_name('BY3EC.sqdOP.L3NKy._8A5w5')
    unfollow_button.click()
    sleep(2.5)
    unnfollow_button = driver.find_element_by_class_name('aOOlW.-Cab_')
    unnfollow_button.click()
    sleep(2.5)

x = 0
while (x < 30):
    thing_you_want_looped()
    x = x + 1

相关问题 更多 >