from selenium import webdriver
import time
driver = webdriver.Chrome()
def login(usr,pss):
driver.get("https://www.instagram.com/")
time.sleep(2)
usr_nm = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
usr_nm.clear()
usr_nm.send_keys(usr)
time.sleep(2)
pss_nm = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
pss_nm.clear()
pss_nm.send_keys(pss)
time.sleep(2)
login = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()
time.sleep(2)
saveinfo = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div/div/section/div/button')
saveinfo.click()
time.sleep(2)
noti = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
noti.click()
time.sleep(2)
def like(hashtag):
driver.get(f"https://www.instagram.com/explore/tags/{hashtag}/")
time.sleep(2)
# for scroll down
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
href_found = driver.find_elements_by_tag_name("a")
pic_href = [ele.get_attribute('href') for ele in href_found if '.com/p' in ele.get_attribute('href')]
for ele in pic_href:
driver.get(ele)
此后,代码无法正常工作,我尝试将其放入循环中,但停止工作并抛出“列表没有属性click()”错误
like = driver.find_elements_by_xpath('/html/body/div[4]/div[2]/div/article/div[3]/section[1]/span[1]/button/div/span')
like.click()
time.sleep(2)
login(<your username >,<your password>)
like('hacking')