2016-12-25 138 views
1

先決條件。
您需要Instagram上的帳戶才能使用此腳本。
設置一個測試環境:
滾動到Selenium Webdriver(Python)

登錄,打開所需的列表(正常工作):

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from time import sleep 

driver = webdriver.Chrome(
# driver = webdriver.Firefox( 
# driver = webdriver.PhantomJS(
    service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 

driver.get("https://instagram.com/accounts/login") 
username = driver.find_element_by_name("username") 
password = driver.find_element_by_name("password") 

username1 = 'instagram' # change it! 
password1 = 'instagrampassword1' # change it! 

username.send_keys(username1) 
password.send_keys(password1) 

submit_button = driver.find_element_by_css_selector(
    '#react-root > div > article > div > div:nth-child(1) > div > form > span > button') 
submit_button.click() 

sleep(2) 

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

driver.implicitly_wait(2) 
driver.find_elements_by_class_name("_218yx")[2].click() 

錯誤滾動。 如何解決此塊?

如何在此頁面上正確對焦和滾動?

我嘗試:

driver.find_element_by_class_name("_cx1ua").send_keys(Keys.NULL) # focus 
     #The element has been deleted entirely or 
     #The element is no longer attached to the DOM. 

driver.find_element_by_class_name("_q44m8").send_keys(Keys.NULL) 
# cannot focus element 

driver.find_element_by_class_name("_qjr85").send_keys(Keys.NULL) 
# cannot focus element 


for i in range(5): 
    driver.find_element_by_class_name("_cx1ua").send_keys(Keys.END) 

==================================== =========================
to @Moshisho:

我們需要關注一些元素來激活它。
問題是我們需要選擇關注的元素以及如何?
這不是一個「體」:
類似的東西,但不是這樣的:

background = driver.find_element_by_css_selector("body") 
# background = driver.find_element_by_css_selector("div._2uju6") 

for i in range(5): 
    background.send_keys(Keys.SPACE) 
    time.sleep(1) 

沒有它,這個命令不工作。


到@Naveen:

print(driver.find_element_by_css_selector("div._a1rcs").location_once_scrolled_into_view) # {'x': 0, 'y': 0} 
print(driver.find_element_by_class_name("_cx1ua").location_once_scrolled_into_view) # {'x': 376, 'y': 229} 
print(driver.find_element_by_class_name("_q44m8").location_once_scrolled_into_view) # {'x': 376, 'y': 180} 
print(driver.find_element_by_class_name("_qjr85").location_once_scrolled_into_view) # {'x': 376, 'y': 180} 

而且接下來會發生什麼?

driver.execute_script("window.scrollTo(0, 3000);") # do not working 

https://www.instagram.com/youtube/following/

+0

您的問題應該作爲題外話閉上。提供一些細節和描述,使你的問題更清楚......不是每個人都有'Instagram':)帳戶 – Andersson

+0

即使你不使用Android/iOs,你也可以註冊。 – lvcpp

+0

Web版本足以完成此任務。 – lvcpp

回答

1

嘗試以下代碼:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from time import sleep 
from selenium.webdriver.support.ui import Select 

driver = webdriver.Chrome(
# driver = webdriver.Firefox( 
# driver = webdriver.PhantomJS(
    service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 

driver.maximize_window() 

driver.get("https://instagram.com/accounts/login") 
username = driver.find_element_by_name("username") 
password = driver.find_element_by_name("password") 

username1 = 'instagramlogin1' # change it! 
password1 = 'instagrampassword1' # change it! 

username.send_keys(username1) 
password.send_keys(password1) 

submit_button = driver.find_element_by_css_selector(
    '#react-root > div > article > div > div:nth-child(1) > div > form > span > button') 
submit_button.click() 

sleep(2) 

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

driver.implicitly_wait(2) 
following = driver.find_element_by_xpath("//a[@href='/youtube/following/']/span") 
total_following = int(following.text) 
print "total no. of users following: ", total_following 
# click on 239 following, displays 10 users 
following.click() 

loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li") 
loaded_till_now = len(loaded_following) 

while(loaded_till_now<total_following): 
    print "following users loaded till now: ", loaded_till_now 
    print loaded_following[loaded_till_now-1] 
    loaded_following[loaded_till_now-1].location_once_scrolled_into_view 
    # driver.execute_script("arguments[0].focus();", loaded_following[loaded_till_now-1]) 
    driver.find_element_by_tag_name('body').send_keys(Keys.END) # triggers AJAX request to load more users. observed that loading 10 users at a time. 
    sleep(1) # tried wihtout sleep but throws StaleElementReferenceException. As it takes time to get the resposne and update the DOM 
    loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li") 
    loaded_till_now = len(loaded_following) 

# All 239 users are loaded. 
driver.quit() 

觀察發現瀏覽器發送AJAX請求加載更多的用戶。當您觸發此動作時scroll using mouse or enter Space or End keys

+0

謝謝,有趣的屬性,但我怎麼可以用它來做需要的東西? 請參閱上面向您解釋的消息。 – lvcpp

+0

我不確定你在說什麼元素(類名是動態的,所以不適合我)。你可以分享屏幕截圖,突出顯示元素和你想要做什麼嗎?點擊或發送密鑰? –

+0

在這種情況下,類名稱是靜態的,只是看起來像動態。 你可以用不同的瀏覽器檢查它。 – lvcpp

0

爲了在窗口中滾動,則需要執行JavaScript,試試這個:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 

編輯:爲了焦點的元素(它需要能夠獲得焦點,例如錨點,輸入,按鈕等......)您還需要使用JavaScript執行程序:

elementToFocus = driver.find_element_by_id("yourID") 
driver.execute_script("arguments[0].focus();", elementToFocus) 
+0

謝謝,我在前面幾個地方看到了這個代碼片斷。 我們需要關注一些元素來激活它。 問題是我們需要選擇哪些元素來關注以及如何處理? 這不是一個 「身體」: 背景= browser.find_element_by_css_selector( 「正文」) #背景= driver.find_element_by_css_selector( 「div._2uju6」) 爲i的範圍(5): background.send_keys( Keys.SPACE) time.sleep(1) 沒有它,此命令不起作用。 – lvcpp

+0

沒有任何帶有ID的元素。
我測試了第二行,但它還沒有工作。
所以我選擇了班,但可能需要選擇類的第二或第三個元素
(我知道該怎麼做,但有很多元素,它看起來像大多數應該是可見和可接受的)。 – lvcpp