0

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']"}

2 Answers2

2

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)
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • no did not work. Instead gave some other error, added the new error to my post – prince saharan Oct 21 '22 at 19:09
  • Now, it's definitely another, new issue. I guess I know what is the problem now. Please share the HTML of that page, not only the single element, you are trying to access. As text, not as a picture and I will try help you more. Also, preferably ask a new question for that since this is a really different issue. – Prophet Oct 22 '22 at 21:14
1

Try this:

from selenium.webdriver.common.by import By

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