Reputation: 13
I am creating a Selenium bot in Python. The driver is supposed to open instagram.com and fill in the login form using the users credentials as an input. There is an error:
# FIRST I OPEN INSTAGRAM LIKE THIS
self.driver.get("https://www.instagram.com/")
# THEN A POP-UP APPEARS, AND I CLOSE IT BY CLICKING IN THE ACCEPT BUTTON,
# IT SEEMS LIKE IT IS NOT AN IFRAME SO NO FRAME SWITCHES ARE NEEDED
cooki = self.driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/button[1]')
cooki.click()
# NOW I TRY TO FIND THE USERNAME FORM TO WRITE IN AN INPUT
usbar = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]') # ERROR HERE
usbar.send_keys(input('Username: '))
The error says:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="loginForm"]/div/div[1]"}
It cannot find the Username form to write in it. I believe there are no iframes in the way, I don't know if there is any other issue preventing the driver to find it. I am fairly new so I can't upload an image of the html of the page yet, that's why I include the line that opens the page. Can anyone come up with a foolproof solution?
Upvotes: 1
Views: 2169
Reputation: 81
You can lookup the full xpatch using inspect in Google Chrome. Your code then becomes:
usbar = driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input')
Upvotes: 0
Reputation: 448
From what I can see there are two problems.
You are trying to call send_keys()
on a div
element. What you are likely looking for is the input
element instead, which is nested inside of that div
element.
This can be obtained by changing your XPATH slightly:
element = self.driver.find_element_by_xpath('//*[@name="username"]')
element.send_keys('...')
Another possible issue is that elements aren't always loaded immediately - you sometimes have to wait for the element to appear in the HTML before finding it. This can be achieved using Webdriver.Wait()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait for the element to appear in the HTML for a maximum of 10 seconds
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.NAME, "username"))
)
# Send input to the element
element.send_keys('...')
Upvotes: 1