I'm new to Selenium and am trying to hack together something that opens a webpage and auto logs-in. I've tried find_element(by=By.NAME, "email").send_keys(username), but get errors around defining "By". Code are errors are below. Could someone offer some guidance on the depracationwarning syntax.
# Used to import the webdriver from selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import os
# Get the path of chromedriver which you have install
def startBot(username, password, url):
s = Service("/usr/local/bin/chromedriver")
# giving the path of chromedriver to selenium webdriver
driver = webdriver.Chrome(service=s)
# opening the website in chrome.
driver.get(url)
# find the id or name or class of
# username by inspecting on username input
driver.find_element_by_name("email").send_keys(username)
# find the password by inspecting on password input
driver.find_element_by_name("password")
# click on submit
#driver.find_element_by_css_selector("button-block").click()
#driver.find_element("submit_btn").click()
driver.find_element_by_xpath("//button[@class='btn btn-primary btn-lg btn-block']").click()
# Driver Code
# Enter below your login credentials
username = "email"
password = "password"
# URL of the login page of site
# which you want to automate login.
url = "https://www.hepdata.net/login/"
# Call the function
startBot(username, password, url)
Errors:
DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead
DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead driver.find_element_by_name("password")
DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead driver.find_element_by_xpath("//button[@class='btn btn-primary btn-lg btn-block']").click()
CodePudding user response:
You are missing the import.
from selenium.webdriver.common.by import By
And then it's just
driver.find_element(By.NAME, "email").send_keys(username)
CodePudding user response:
UPDATE:
I made some edits and managed to get the below edits working:
# Find the id/name/class of username by inspecting on username input
driver.find_element(By.NAME, "email").send_keys(username)
# find the password by inspecting on password input
driver.find_element(By.NAME, "password").send_keys(password)
# click on submit
driver.find_element(By.XPATH, "//button[@class='btn btn-primary btn-lg btn-block']").click()