AttributeError: object has no attribute in python in selenium webdriver - python

While I am intermediate in python automation, I am trying to run a program, but unable to do it so, as I am seeing this
AttributeError: object has no attribute.
my Code is like this:
from selenium import webdriver
import os
import unittest
class goodsweb(unittest.TestCase):
def setting(self):
driverlocation ="/Users/new/Documents/Learning/Drivers/selenium-2.53.1/py/selenium/webdriver/chromedriver"
os.environ["webdriver.chrome.driver"] = driverlocation
self.driver = webdriver.Chrome(driverlocation)
driver = self.driver
url = "https://goods.theentertainerme.com/"
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)
def test_data(self):
driver =self.driver
self.RegisterButton = driver.find_element_by_xpath("//*[#id='logs']/li[2]//a[text()='Sign up']")
self.FirstName = driver.find_element_by_id("registrationform-firstname")
self.Lastname = driver.find_element_by_id("registrationform-lastname")
self.EmailAddress = driver.find_element_by_id("registrationform-email")
self.CountryofResidence = driver.find_element_by_id("registrationform-country_of_residence")
self.DateofBirth = driver.find_element_by_id("registrationform-date_of_birth")
self.Gender = driver.find_element_by_id("registrationform-gender")
self.Password = driver.find_element_by_id("registrationform-password")
self.ConfirmPassword = driver.find_element_by_id("registrationform-confirmpassword")
def test_register(self):
driver = self.driver
self.RegisterButton.click()
self.FirstName.send_keys("Taimoor")
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
and this is the error, which i am seeing.
line 38, in tearDown
self.driver.close()
AttributeError: 'goodsweb' object has no attribute 'driver'
Please help me out here with explaination, that where I am making mistake.

Try to rename def setting to def setUp.
setUp is a method of unittest.TestCase class that you want to override with your settings.

Related

Error: AttributeError: 'WebElement' object has no attribute 'Clear' in Selenium with Python

I am getting this error while trying to run the below code in a demo site:
self.driver.find_element(By.XPATH, "//input[#id='txtUsername']").Clear()
"AttributeError: 'WebElement' object has no attribute 'Clear'"
I create a POM format with 2 .py files but I can't get the root cause of the error.
This is the main class:
import time
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from POMProjectDemo.Pages.LoginPage import LoginPage
from POMProjectDemo.Pages.HomePage import HomePage
class LoginTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
s = Service("C:/drivers/chromedriver.exe")
cls.driver = webdriver.Chrome(service=s)
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
def test_login_valid(self):
driver = self.driver
self.driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login")
login = LoginPage(driver)
login.enter_username("Admin")
login.enter_password("admin123")
login.click_login()
#classmethod
def tearDownClass(cls):
cls.driver.close()
cls.driver.quit()
print("Test Completed")
This is the LoginPage:
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username_textbox_xpath = "//input[#id='txtUsername']"
self.password_textbox_cssSelector = "input[type='password']"
self.login_button_xpath = "//input[#value='LOGIN']"
def enter_username(self, username):
self.driver.find_element(By.XPATH, self.username_textbox_xpath).Clear()
self.driver.find_element(By.XPATH, self.username_textbox_xpath).send_keys(username)
def enter_password(self, password):
self.driver.find_element(By.CSS_SELECTOR, self.password_textbox_cssSelector).Clear()
self.driver.find_element(By.CSS_SELECTOR, self.password_textbox_cssSelector).send_keys(password)
def click_login(self):
self.driver.find_element(By.XPATH, self.login_button_xpath).Click()
Any thoughts on this error?
These methods are case sensitive
Wrong Case : .Click() and .Clear() // which you have used instead use
Correct Case : .click() and .clear()

Not able to launch browser using python selenium

I am trying to inherit browser class in test. Can someone please point out what am i doing wrong here. I am new to python
This is my test class where I am trying to inherit browser class
import unittest
from Configurations.Browser import Browser
class GoogleTest(Browser):
def test_homepage(self):
driver = self.driver
self.driver.implicitly_wait(10) self.driver.find_element_by_xpath("/html/body/div/div[4]/form/div[2]/div[1]/div[1]/div/div[2]/input").send_keys("Test")
Browser.py:
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executablepath=r"C:\Setups\Selenium\Drivers\geckodriver.exe")
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.driver.get("https://www.google.com")
def tearDown(self):
if(self.driver != None):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
You have to change the "excutablepath" to "executable_path".(Browser.py)
like this
self.driver = webdriver.Firefox(executable_path=r"C:\Setups\Selenium\Drivers\geckodriver.exe")

Why getting error AttributeError: 'WebDriver' object has no attribute 'executes_script'

Getting error for line self.driver.executes_script
self.driver.executes_script("arguments[0].click();", new_notification)
AttributeError: 'WebDriver' object has no attribute 'executes_script'
None
but their is a class name with it
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import HtmlTestRunner
import time
class Environment(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="F:\\automation\\chromedriver.exe")
# login test case
def test_first_page_login(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost/dashboard/user/login")
self.driver.find_element_by_id('uemail').send_keys('xyz#abc.com')
self.driver.find_element_by_id('upwd').send_keys('1234567890')
self.driver.find_element_by_id('upwd').send_keys(Keys.RETURN)
# login page going
def test_going_notification_page(self):
self.test_first_page_login()
time.sleep(5)
going_noti_page = self.driver.find_element_by_class_name('caret')
print(self.driver.execute_script("arguments[0].click();", going_noti_page))
new_notification = self.driver.find_element_by_class_name('fa-paper-plane')
self.driver.executes_script("arguments[0].click();", new_notification)
time.sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='F:\\automation\\reports'))
you just have a typo:
self.driver.executes_script("arguments[0].click();", new_notification)
executes_script doesn't exist, but based on the api-doc you probably mean: execute_script
DOC: WebDriver.execute_script

Python Selenium 'module' object is not callable in python selenium script

Learning Selenium driven by Python and in my practice I keep getting the following error. I am stuck and could use some guidance
Traceback (most recent call last): File "test_login.py", line 14, in
test_Login
loginpage = homePage(self.driver) TypeError: 'module' object is not callable
Here is my code
test_login.py
import unittest
import homePage
from selenium import webdriver
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("https://hub.docker.com/login/")
def test_Login(self):
loginpage = homePage(self.driver)
loginpage.login(email,password)
def tearDown(self):
self.driver.close()
if __name__ == '__main__': unittest.main()
homePage.py
from selenium.webdriver.common.by import By
class BasePage(object):
def __init__(self, driver):
self.driver = drive
class LoginPage(BasePage):
locator_dictionary = {
"userID": (By.XPATH, '//input[#placeholder="Username"]'),
"passWord": (By.XPATH, '//input[#placeholder="Password"]'),
"submittButton": (By.XPATH, '//button[text()="Log In"]'),
}
def set_userID(self, id):
userIdElement = self.driver.find_element(*LoginPage.userID)
userIdElement.send_keys(id)
def login_error_displayed(self):
notifcationElement = self.driver.find_element(*LoginPage.loginError)
return notifcationElement.is_displayed()
def set_password(self, password):
pwordElement = self.driver.find_element(*LoginPage.passWord)
pwordElement.send_keys(password)
def click_submit(self):
submitBttn = self.driver.find_element(*LoginPage.submitButton)
submitBttn.click()
def login(self, id, password):
self.set_password(password)
self.set_email(id)
self.click_submit()
Any help is appreciated
I think here:
loginpage = homePage(self.driver)
you meant to instantiate the LoginPage class:
loginpage = homePage.LoginPage(self.driver)

Python WebDriver AttributeError: LoginPage instance has no attribute 'driver'

I have read a few tutorials on Python Selenium Webdriver Page Object model as I have to automate the gui tests using Selenium with Python.
To start off with I am trying to write a Login Page class and a LoginMainTest class. I am getting the following error when i run the code.
AttributeError: LoginPage instance has no attribute 'driver'
I think i have to specify the selenium driver where i instantiate the LoginPage
e.g. on this line log_in = LoginPage.LoginPage()
I need some help please.
Full error:
Traceback (most recent call last):
File "E:\Python projects\unitTest_sample - Modifying into Page Object\LoginMainTest.py", line 11, in test_valid_login
log_in = LoginPage.LoginPage()
File "E:\unitTest_sample - Modifying into Page Object\LoginPage.py", line 20, in __init__
emailFieldElement = self.driver.find_element_by_id(self.emailFieldID)
AttributeError: LoginPage instance has no attribute 'driver'
My LoginMainTest.py class is as follows:
import LoginPage
import unittest
class GoogleTest(unittest.TestCase):
def test_valid_login(self):
log_in = LoginPage.LoginPage()
log_in.userLogin_valid()
if __name__ == '__main__':
unittest.main()
My Login.py class is as follows:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
class LoginPage():
Username = "test1"
password = "Test"
emailFieldID = "email"
passFieldID = "pass"
loginButtonXpath = "//input[#value='log in']"
logo_xpath = "//a[contains(#href, 'logo')])[1]"
def setup(self):
self.driver = webdriver.Firefox()
self.driver.get("http://www.testaaa.com")
def __init__(self):
emailFieldElement = self.driver.find_element_by_id(self.emailFieldID)
passFieldElement = self.driver. find_element_by_id(self.passFieldID)
loginFieldElement = self.driver.find_element_by_xpath(self.loginButtonXpath)
def userLogin_valid(self):
self.emailFieldElement.clear()
self.emailFieldElement.send_keys(self.Username)
self.passFieldElement.clear()
self.send_keys(self.password)
self.loginFieldElement.click()
def tearDown(self):
self.driver.quit()
Firstly, there is a flaw in your design.
The reason your script is failing because when you create the object of login page the init gets called but it fails to find the driver since it is defined in the setup fn (which is never called)
Ideally in the page object model you should initialize your browser(driver) in your test file and then while creating a object of any page file you should pass that driver.
Your setup should look something like this,
Page file:
# setup() fn not needed here
.
.
def __init__(self, driver):
self.driver = driver
emailFieldElement = self.driver.find_element_by_id(self.emailFieldID)
passFieldElement = self.driver. find_element_by_id(self.passFieldID)
loginFieldElement = self.driver.find_element_by_xpath(self.loginButtonXpath)
.
# teardown() not needed here, should be in test file
.
Test File:
.
.
class GoogleTest(unittest.TestCase):
def test_valid_login(self):
self.driver = webdriver.Firefox() # the first 2 stmts can be in a setupclass
self.driver.get("http://www.testaaa.com")
log_in = LoginPage.LoginPage(self.driver)
log_in.userLogin_valid()
.
.
I've had this issue a couple of times and every time I found it due to a mismatch between my Chrome browser version and the Chrome Webdriver version.
So, in your Chrome browser check Help>About Google Chrome before downloading a corresponding ChromeDriver from https://chromedriver.chromium.org/.
Good luck!

Categories