Home > Back-end >  i am trying to run this code and getting error AttributeError: 'WebDriver' object has no a
i am trying to run this code and getting error AttributeError: 'WebDriver' object has no a

Time:10-11

tring this codebut throwing an attribute error
driver: WebDriver = webdriver.Chrome("C:\Drivers\chromedriver_win32\chromedriver.exe")

driver.get("https://opensource-demo.orangehrmlive.com/")
driver.find_element_by_name("txtUsername").send_keys("Admin")
driver.find_element_by_id("txtPassword").send_keys("admin123")
driver.find_element_by_id("btnLogin").click()

act_title=driver.title
exp_title="OrangeHRM"

if act_title==exp_title:
    print("Login test pass")
else:
    print("Login test fail")

driver.close()

CodePudding user response:

Just do this:

driver = webdriver.Chrome('C:\Drivers\chromedriver_win32\chromedriver.exe')

There is no need to declare WebDriver as driver.

CodePudding user response:

The name attribute for the User Name element is username.

The name attribute for the Password element is password.

Otherwise, you can use other locators like xpath like -

//input[@placeholder='Username']

CodePudding user response:

All the methods like find_element_by_name, find_element_by_xpath, find_element_by_id etc. are deprecated now.
You should use find_element(By. instead.
So, your code can be as following:

driver.get("https://opensource-demo.orangehrmlive.com/")
driver.find_element(By.NAME, "txtUsername").send_keys("Admin")
driver.find_element(By.ID, "txtPassword").send_keys("admin123")
driver.find_element(By.ID, "btnLogin").click()

act_title=driver.title
exp_title="OrangeHRM"

if act_title==exp_title:
    print("Login test pass")
else:
    print("Login test fail")

driver.close()

Also, you should add delays in the above code. WebDriverWait should be used for that

CodePudding user response:

you are accessing wrong attribute that is not on the webpage or maybe you have written name of attributes and there value try CSS selector or XPath Example: driver.findElement(By.xpath("//TagName[@attribute='value']")); or driver.findElement(By.cssSelector(""));

  • Related