【发布时间】:2019-12-05 17:51:39
【问题描述】:
问题是它不喜欢这些帖子。
我已经尝试过像标签名称这样的差异化方法
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def like_photo(self):
driver = self.driver
driver.get("https://www.instagram.com")
time.sleep(1)
for i in range(1, 4):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
# find all the heart links
hrefs = driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
pic_hrefs = [href for href in pic_hrefs]
print(' Photos ' + str(len(pic_hrefs)))
for _ in pic_hrefs:
driver.get("https://www.instagram.com")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
like_button = lambda: driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
like_button.click()
time.sleep(18)
except Exception as e:
time.sleep(1)
nameIG = InstagramBot(username, password)
nameIG.login()
nameIG.like_photo()
它与任何帖子一样,输出只是:照片 4
进程以退出代码 0 结束
【问题讨论】:
-
请注意,一个好的堆栈溢出问题是关于一个特定问题的,只需要最短的代码来隔离该问题。因此,在一个理想的世界中,在提出问题之前,可以隔离出不工作的特定 XPath 查询,并编写其他人可以运行的最短测试工具,以亲眼看看它不工作。