Reputation: 11
I'm trying to do some web-scraping on instagram with selenium. specifically i'm trying to log-in by this address https://www.instagram.com/accounts/login/ with selenium.
On this page, the input 'username' is written like this
<input class="_ph6vk _o716c" aria-describedby="" aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="30" name="username" placeholder="Phone number, username, or email" value="" type="text">
What i'm doing in python is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Firefox()
driver.get('https://www.instagram.com/accounts/login/')
input_username = driver.find_elements_by_xpath("//input[@name='username']")
input_username.send_keys("username")
Python returns me : AttributeError: 'list' object has no attribute 'send_keys'
So I did the same changing this:
input_username[0].send_keys("username")
And the error is:
IndexError: list index out of range
So, the array is empty. Anyone knows how to solve it?
Upvotes: 0
Views: 1985
Reputation: 21
The form loads after the page is loaded so here's how I did it :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get("https://instagram.com")
time.sleep(4)
driver.find_element_by_name("username").send_keys("foobar")
driver.find_element_by_name("password").send_keys("pass")
driver.find_element_by_name("password").send_keys(Keys.ENTER)
A bit hacky at the end
Upvotes: 0
Reputation: 7504
In your case the page might not have loaded the form, so use WebDriverWait
to let the element load and start scraping.
You can check for the element this way, instead of putting time.sleep(2)
because it might take a long time to load it as well.
myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
Also try to use the api if it's possible, scraping should be the second approach.
Upvotes: 1
Reputation: 12669
Try this code:
from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
user_name=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[1]/div/input')
user_name.send_keys('user_name')
password=driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/input')
password.send_keys('pa$$')
driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/span[1]/button').click()
Upvotes: 0