prince saharan
prince saharan

Reputation: 1

AttributeError Py

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'

getting this error now.

Traceback (most recent call last): File "C:\Users\admin\Desktop\pyhton\index.py", line 13, in driver.find_element(By.XPATH, "//input[@placeholder='Password']").send_keys(PASSWORD) File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute self.error_handler.check_response(response) File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@placeholder='Password']"}

Upvotes: 0

Views: 94

Answers (2)

Prophet
Prophet

Reputation: 33361

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)

Upvotes: 2

kotschi123
kotschi123

Reputation: 33

Try this:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, "//input[@placeholder='Password']").send_keys(PASSWORD)

Upvotes: 1

Related Questions