pythonselenium-webdriverwebdriverpytestbrowser-automation

WebDriver.find_element_by_id() takes 1 positional argument but 2 were given


from selenium import webdriver
import time
driver = webdriver.Chrome() 
driver.implicitly_wait(10)
driver.maximize_window()
driver.get("https://opensource-demo.orangehrmlive.com/")
driver.find_element_by_id('username').send_keys("Admin")
driver.find_element_by_id('password').send_keys("admin123")
driver.find_element_by_link_text('login').click()
time.sleep(2)
driver.close()
driver.quit()
print("Test Completed")

** Error (Problem): **

Traceback (most recent call last):
 username_field = driver.find_element_by_id('username')
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: WebDriver.find_element_by_id() takes 1 positional argument but 2 were given

I tried to add method (find_element_by_id) in the custom webdriver because I was unable to find it in the test.py file so I created it by myself. But in the end, I am encountering the error(problem) that is stated above.

Do I have to add the libraries to find the element by id, or does my web driver need to update? I am new and learning this. If somebody helps me, it is appreciated.


Solution

  • The .find_element_by_id() syntax was deprecated a while back. The current syntax is

    from selenium.webdriver.common.by import By
    
    driver.find_element(By.ID, "username")
    

    You should familiarize yourself with the docs for this and more ways to find elements and other Selenium functionality.