Prerequisites.
You need an account at Instagram to use this script.
Setup a test environment:
Log in, open the needed list(works correctly):
JavaScript
x
31
31
1
from selenium import webdriver
2
from selenium.webdriver.common.keys import Keys
3
from time import sleep
4
5
driver = webdriver.Chrome(
6
# driver = webdriver.Firefox(
7
# driver = webdriver.PhantomJS(
8
service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
9
10
driver.get("https://instagram.com/accounts/login")
11
username = driver.find_element_by_name("username")
12
password = driver.find_element_by_name("password")
13
14
username1 = 'instagram' # change it!
15
password1 = 'instagrampassword1' # change it!
16
17
username.send_keys(username1)
18
password.send_keys(password1)
19
20
submit_button = driver.find_element_by_css_selector(
21
'#react-root > div > article > div > div:nth-child(1) > div > form > span > button')
22
submit_button.click()
23
24
sleep(2)
25
26
link = 'https://www.instagram.com/youtube/'
27
driver.get(link)
28
29
driver.implicitly_wait(2)
30
driver.find_elements_by_class_name("_218yx")[2].click()
31
Wrong scroll. How to fix this block?
How to focus and scroll correctly on this page?
My attempts:
JavaScript
1
14
14
1
driver.find_element_by_class_name("_cx1ua").send_keys(Keys.NULL) # focus
2
#The element has been deleted entirely or
3
#The element is no longer attached to the DOM.
4
5
driver.find_element_by_class_name("_q44m8").send_keys(Keys.NULL)
6
# cannot focus element
7
8
driver.find_element_by_class_name("_qjr85").send_keys(Keys.NULL)
9
# cannot focus element
10
11
12
for i in range(5):
13
driver.find_element_by_class_name("_cx1ua").send_keys(Keys.END)
14
=============================================================
to @Moshisho :
We need to focus on some element to activate it.
The question is what the element we need to choose to focus and how?
This is not a “body”:
something like that, but not this:
JavaScript
1
7
1
background = driver.find_element_by_css_selector("body")
2
# background = driver.find_element_by_css_selector("div._2uju6")
3
4
for i in range(5):
5
background.send_keys(Keys.SPACE)
6
time.sleep(1)
7
Without it this command do not work.
to @Naveen :
JavaScript
1
5
1
print(driver.find_element_by_css_selector("div._a1rcs").location_once_scrolled_into_view) # {'x': 0, 'y': 0}
2
print(driver.find_element_by_class_name("_cx1ua").location_once_scrolled_into_view) # {'x': 376, 'y': 229}
3
print(driver.find_element_by_class_name("_q44m8").location_once_scrolled_into_view) # {'x': 376, 'y': 180}
4
print(driver.find_element_by_class_name("_qjr85").location_once_scrolled_into_view) # {'x': 376, 'y': 180}
5
And what’s next?
JavaScript
1
2
1
driver.execute_script("window.scrollTo(0, 3000);") # do not working
2
Advertisement
Answer
Try the following code:
JavaScript
1
54
54
1
from selenium import webdriver
2
from selenium.webdriver.common.keys import Keys
3
from time import sleep
4
from selenium.webdriver.support.ui import Select
5
6
driver = webdriver.Chrome(
7
# driver = webdriver.Firefox(
8
# driver = webdriver.PhantomJS(
9
service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
10
11
driver.maximize_window()
12
13
driver.get("https://instagram.com/accounts/login")
14
username = driver.find_element_by_name("username")
15
password = driver.find_element_by_name("password")
16
17
username1 = 'instagramlogin1' # change it!
18
password1 = 'instagrampassword1' # change it!
19
20
username.send_keys(username1)
21
password.send_keys(password1)
22
23
submit_button = driver.find_element_by_css_selector(
24
'#react-root > div > article > div > div:nth-child(1) > div > form > span > button')
25
submit_button.click()
26
27
sleep(2)
28
29
link = 'https://www.instagram.com/youtube/'
30
driver.get(link)
31
32
driver.implicitly_wait(2)
33
following = driver.find_element_by_xpath("//a[@href='/youtube/following/']/span")
34
total_following = int(following.text)
35
print "total no. of users following: ", total_following
36
# click on 239 following, displays 10 users
37
following.click()
38
39
loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li")
40
loaded_till_now = len(loaded_following)
41
42
while(loaded_till_now<total_following):
43
print "following users loaded till now: ", loaded_till_now
44
print loaded_following[loaded_till_now-1]
45
loaded_following[loaded_till_now-1].location_once_scrolled_into_view
46
# driver.execute_script("arguments[0].focus();", loaded_following[loaded_till_now-1])
47
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.
48
sleep(1) # tried wihtout sleep but throws StaleElementReferenceException. As it takes time to get the resposne and update the DOM
49
loaded_following = driver.find_elements_by_xpath("//ul[@class='_539vh _4j13h']/li")
50
loaded_till_now = len(loaded_following)
51
52
# All 239 users are loaded.
53
driver.quit()
54
Observed that browser is sending AJAX request to load more users. this action is triggered when you scroll using mouse or enter Space or End keys