New to Python Selenium.
I am trying to create an script to login to my home router and press the button restart.
Running to error, when trying to login to the router, can some on guide on my mistake here.
below is the code and also attaching the .screenshot
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
PASSWORD = 'testtes'
login_page = 'http://192.168.2.1/login.html'
driver.get(login_page)
driver.find_element_by_xpath("//input[@placeholder='Password']").send_keys(PASSWORD)
Below is the error I am getting.
Traceback (most recent call last): File "C:\Users\admin\Desktop\pyhton\index.py", line 14, in driver.find_element_by_xpath("//input[@placeholder='Password']").send_keys(PASSWORD) AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
CodePudding user response:
Probably you are using Selenium 4. if so, find_element_by_xpath
and all the others find_element_by_*
methods are not supported by Selenium 4, you have to use the new syntax and add an essential import, as following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
PASSWORD = 'testtes'
login_page = 'http://192.168.2.1/login.html'
driver.get(login_page)
driver.find_element(By.XPATH, "//input[@placeholder='Password']").send_keys(PASSWORD)
CodePudding user response:
Try this:
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, "//input[@placeholder='Password']").send_keys(PASSWORD)