第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Selenium 在 Instagram 彈出框架中向下滾動

如何使用 Selenium 在 Instagram 彈出框架中向下滾動

躍然一笑 2021-10-10 16:46:54
我有一個 python 腳本,使用 selenium 轉(zhuǎn)到給定的 Instagram 個人資料并遍歷用戶的關(guān)注者。在 instagram 網(wǎng)站上,當單擊查看關(guān)注者列表時,會打開一個彈出窗口,其中列出了列出的帳戶(這是該網(wǎng)站的屏幕截圖)然而,無論是在視覺上還是在 html 中,都只顯示了 12 個帳戶。為了看到更多,必須向下滾動,所以我嘗試使用 Keys.PAGE_DOWN 輸入來執(zhí)行此操作。from selenium import webdriverfrom selenium.common.exceptions         import TimeoutExceptionfrom selenium.webdriver.support.ui      import WebDriverWait from selenium.webdriver.support         import expected_conditions as ECfrom selenium.webdriver.chrome.options  import Optionsfrom selenium.webdriver.common.keys     import Keysimport time ...username = 'Username'password = 'Password'message  = 'blahblah'tryTime  = 2#create driver and log indriver = webdriver.Chrome()logIn(driver, username, password, tryTime)#gets rid of preference pop-upa = driver.find_elements_by_class_name("HoLwm")a[0].click()#go to profiledriver.get("https://www.instagram.com/{}/".format(username))#go to followers listfollowers = driver.find_element_by_xpath("//a[@href='/{}/followers/']".format(username))followers.click()time.sleep(tryTime) #find all li elements in listfBody  = driver.find_element_by_xpath("//div[@role='dialog']")fBody.send_keys(Keys.PAGE_DOWN) fList  = fBody.find_elements_by_tag("li")print("fList len is {}".format(len(fList)))time.sleep(tryTime)print("ended")driver.quit()當我嘗試運行它時,出現(xiàn)以下錯誤:Message: unknown error: cannot focus element我知道這可能是因為我為 使用了錯誤的元素fBody,但我不知道哪個是正確的。有誰知道我應該將 PAGE_DOWN 鍵發(fā)送到哪個元素,或者是否有另一種加載帳戶的方法?任何幫助深表感謝!
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻1911條經(jīng)驗 獲得超7個贊

如果在 range(1, 4) 中為 i 的范圍添加迭代 (for),則上述代碼工作正常:嘗試:


            #find all li elements in list

            fBody  = self.driver.find_element_by_xpath("//div[@class='isgrP']")

            scroll = 0

            while scroll < 5: # scroll 5 times

                self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)

                time.sleep(2)

                scroll += 1


            fList  = self.driver.find_elements_by_xpath("//div[@class='isgrP']//li")

            print("fList len is {}".format(len(fList)))


        except Exception as e:

            print(e, "canot scrol")


        try:

            #get tags with a

            hrefs_in_view = self.driver.find_elements_by_tag_name('a')

            # finding relevant hrefs

            hrefs_in_view = [elem.get_attribute('title') for elem in hrefs_in_view]


            [pic_hrefs.append(title) for title in hrefs_in_view if title not in pic_hrefs]

            print("Check: pic href length " + str(len(pic_hrefs)))


        except Exception as tag:

            print(tag, "can not find tag")

因此,即使 while 循環(huán)未命中,for 循環(huán)也可以滾動


查看完整回答
反對 回復 2021-10-10
  • 2 回答
  • 0 關(guān)注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號