1

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?

1
  • Have you checked the page source which your program ends up with? Commented Oct 17, 2020 at 0:57

2 Answers 2

1

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('...')
Sign up to request clarification or add additional context in comments.

2 Comments

I changed the XPath to the input as you said. To be honest that's what I did in the first place, but the error arised so I tried finding another path, and that's what I showed. I'm using the WebDriverWait() as you said, but now it says (NameError: name 'By' is not defined). Also, I changed By.NAME with By.XPATH, and used the path to the input. I guess with that, the find_element_by_xpath I wrote in the original is not needed right?
@VektorElektor you get NameError because you have to import By from selenium.webdriver.common.by (see the top of the second code snippet). And yes, the find_element_by_xpath function is not needed if you use WebDriverWait because that will return the element once the element appears in the HTML.
0

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') 

enter image description here

1 Comment

That is how im looking for the Xpaths, and I have already tried 'Copy full XPath' instead of 'Copy XPath', but to no avail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.