我正在尝试获取指定帐户的关注者列表,但是当我打开关注者弹出窗口时,一切正常,只是有时程序会随机单击人们的帐户,然后程序崩溃。
我搜索了很多,他们谈论instagram的结构。我看到像instagram这样的注释在会话期间更改了它的元素,因此是因为我的程序单击了错误的元素,但是我用xpath展示了方法。 有人说,ajax导致了这种情况,我试图在2或3天内弄清楚这一点,我很累。 如果您有兴趣,我将在下面保留我代码的获取列表部分。
def get_followers(self,target):
#GOING TARGETS URL
self.driver.get(f"{self.url}/{target}/")
time.sleep(2)
self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a').click()
time.sleep(1)
dialog=self.driver.find_element_by_css_selector('.isgrP ul')
#dialog=self.driver.find_element_by_css_selector('.pbNvD.fPMEg.HYpXt ul')
#folC= len(dialog.find_elements_by_css_selector("li"))
action = webdriver.ActionChains(self.driver)
fc=self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a/span').text
down=0
for x in range(1,math.ceil(int(fc)/12)):
if down!=math.ceil(int(fc)/12):
print(down,math.ceil(int(fc)/12))
down+=1
dialog.click()
action.key_down(Keys.END).key_up(Keys.END).perform()
time.sleep(0.4)
flist=self.driver.find_elements_by_css_selector(".pbNvD.fPMEg.HYpXt a")
print(flist)
#followers=dialog.find_elements_by_css_selector("li a")
for follower in flist:
print(follower.text)
我认为问题是 HERE !!
action = webdriver.ActionChains(self.driver)
#HOW MANY FOLLOWERS TARGET HAVE ? FOR EX 548
fc=self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a/span').text
down=0
#SCROLLING DOWN TO THE BOTTOM
for x in range(1,math.ceil(int(fc)/12)):
if down!=math.ceil(int(fc)/12):
print(down,math.ceil(int(fc)/12))
down+=1
dialog.click()
action.key_down(Keys.END).key_up(Keys.END).perform()
time.sleep(0.4)
# AFTER FINISHING, GETTING ALL LIST ELEMENTS ASS YOU CAN SEE ON THE CODE UP THERE
# BUT WHEN IT SCROLLS IT CLICKS SOMEONE'S PROFILE AND GOES THERE
# BECAUSE OF THAT PROGRAM CRASHES