简体   繁体   中英

Finding elements using selenium in python

I am trying to find and then fill in the username and password of Instagrams login page using:

from selenium.webdriver.common.keys import Keys
import os
from selenium import webdriver
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'Chromedriver')
driver = webdriver.Chrome(executable_path = filename)
driver.get("https://www.instagram.com")
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")

username.send_keys("blankspace")
password.send_keys("blankspace")

however, I keep getting an error that it cant detect the element even though the name is correct

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}

The HTML:

<input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value="">

You have to close the cookie consent popup first before accessing the form fields:

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'Chromedriver')
driver = webdriver.Firefox()
driver.get("https://www.instagram.com")

WebDriverWait(driver, 5).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, "button.aOOlW.bIiDR"))
)
driver.find_element(By.CSS_SELECTOR, "button.aOOlW.bIiDR").click()

username = driver.find_element(By.NAME, "username")
password = driver.find_element(By.NAME, "password")

username.send_keys("blankspace")
password.send_keys("blankspace")

driver.close()

Also consider using the updated methods find_element() :

DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instea

I would check out Selenium documentation! Link . By going off the docs you could try the following:

username = browser.find_element(By.NAME, "username")
password = browser.find_element(By.NAME, "password")
username.send_keys("text")
password.send_keys("text")

Though I would use XPATH instead as it would be quicker:

username = browser.find_element(By.XPATH, '//[@id="loginForm"]/div/div[1]/div/label/input')
password = browser.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[2]/div/label/input')
username.send_keys("text")
password.send_keys("text")

Enjoy!

The answer to anyone who cares to know is that Instagram loads in the body of the page after you get to it so you just have to add a delay before you start looking for the elements. Thats to @edd for pointing that out

driver.get("https://www.instagram.com")
time.sleep(5)
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")

username.send_keys("blankspace")
password.send_keys("blankspace")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM