AttributeError: MainPage object has no attribute 'execute' - python

I am using Unittest + Pytest. I need perfrom move over.
Here is first page:
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
import unittest
from page.home.main_page import MainPage
from selenium.webdriver import ActionChains
class MainPageTests(unittest.TestCase):
def setUp(self):
baseURL = "https://grin.co/"
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(3)
driver.get(baseURL)
self.mb = MainPage(driver)
self.driver = driver
#pytest.mark.run(order=1)
def testtitle(self):
self.mb.creatormenu()
Here is second:
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time
class MainPage():
def init(self,driver):
self.driver = driver
def creatormenu(self):
time.sleep(2)
element = self.driver.find_element(By.ID, "menu-item-17096")
actions = ActionChains(self)
actions.move_to_element(element).perform()
print("Mouse Hovered on element")
And I gets error:
device.actions = []
self.driver.execute(Command.W3C_ACTIONS, enc)
E AttributeError: 'MainPage' object has no attribute 'execute'
venv/lib/python3.8/site-packages/selenium/webdriver/common/actions/action_builder.py:88: AttributeError
======================================================================================= short test summary info ========================================================================================
FAILED Tests/home/mane_page_test.py::MainPageTests::testtitle - AttributeError: 'MainPage' object has no attribute 'execute'
What did I miss?
Thank you in advance

Thank you for posting your answer Oleg!
I had the same issue when tried to use the following functions without ".driver":
def switchToNewWindow(self):
remote = Remote(self.driver)
remote.switch_to.window(remote.window_handles[1])
and
def moveSlideBar(self, Xamount, Yamount, locator):
move = ActionChains(self.driver)
move.click_and_hold(locator).move_by_offset(Xamount, Yamount).release().perform()

Related

Selenium webdriver not recognized as webdriver

So I have this test and fixture:
#pytest.fixture()
def init_driver():
return webdriver.Chrome(executable_path=TestParams.CHROME_EXECUTABLE_PATH)
#pytest.mark.usefixtures('init_driver')
def test_1():
driver = init_driver
login(driver)
In this case my driver type is <class 'function'> so I cannot use its function for example get and got this error:
AttributeError: 'function' object has no attribute 'get'
When I change it to this:
#pytest.mark.usefixtures('init_driver')
def test_1():
login(webdriver.Chrome(executable_path=TestParams.CHROME_EXECUTABLE_PATH))
My driver type is <selenium.webdriver.chrome.webdriver.WebDriver (session="4ca3cb8f5bcec7a7498a65bfe5a2ea81")>
And all works fine.
What I am doing wrong ?
I fixed the syntax and changed it so that it runs without your hidden methods and variables such as login() and TestParams:
import pytest
from selenium import webdriver
#pytest.fixture()
def init_driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
def test_1(init_driver):
driver = init_driver
print(type(driver))
Running that prints:
<class 'selenium.webdriver.chrome.webdriver.WebDriver'>

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()

NameError: name 'driver' is not defined

Well, I'm studying Python for the first time. I need help with this code below:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class CursoAutomacao:
def __init__(self):
chrome_options = Options()
chrome_options.add_argument('--lang-pt-BR')
driver = webdriver.Chrome(executable_path=r'C:\Users\Daniel pc\Desktop\Tutorial chromeDriver\chromedriver.exe')
def Iniciar(self):
self.driver.get('https://www.mercadolivre.com.br')
curso = CursoAutomacao()
curso.Iniciar()
When I run this code this error appears: NameError: **name 'driver' is not defined**.
What could be happening? My window of Chrome opens but close very quick.
Thanks in advance for your help!
You need to use self.driver = ... in your init method.
In totality:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class CursoAutomacao:
def init(self):
chrome_options = Options()
chrome_options.add_argument('--lang-pt-BR')
self.driver = webdriver.Chrome(executable_path=r'C:\Users\Daniel\pc\Desktop\Tutorial\chromeDriver\chromedriver.exe')
def Iniciar(self):
self.driver.get('https://www.mercadolivre.com.br')
curso = CursoAutomacao()
curso.Iniciar()

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

my button click to return the next page object is showing the error module object is not callable

I have started using the page object model in my automation script for our website. I am using Python, Webdriver
I have a LoginPage class with some methods
I have a AdministrationPage class with a method. (When user has logged in the Administration page is shown)
On the Login page I have a method called clickAdministration(self):. the button click is in here and the return is return Pages.administrationPage(self.driver)
When my code runs and it gets to the clickAdministration the following error is shown:
Traceback (most recent call last):
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore \TestCases\AdministrationPage_TestCase.py", line 31, in test_add_Project
administration_page = login_page.clickAdministration()
File "C:\Users\riaz.ladhani\PycharmProjects\Selenium Webdriver\ClearCore \Pages\page.py", line 59, in clickAdministration
return Pages.administrationPage(self.driver)
TypeError: 'module' object is not callable
I need some help please, my syntax is incorrect for calling the Administration Page object when i click the Administraition button from the LoginPage class.
When i click the Administration link after the user has logged in (from LoginPage class) I should return the AdministrationPage object.
My LoginPage and AdministrationPage classes are in a Package called Pages
My TestCase Classes are in a Package called TestCases
Also you will notice in my code snippet my calls to WebDriverWait is commented out. This is because I get a timeout exception when i use this. I have reverted to using time.sleep for now.
If anyone can also help with my WebDriverWait syntax that would be helpful.
Thank you.
My code snipped as follows:
TestCases\AdministrationPage_TestCase.py
import unittest
import time
from selenium import webdriver
from Locators import locators
from Locators import element
from Pages import page
from Pages.administrationPage import AdministrationPage
from Pages.page import LoginPage
class AdministrationPage_TestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Ie("C:\QA\Automation\Python_projects\Selenium Webdriver\IEDriverServer_Win32_2.45.0\IEDriverServer.exe")
self.driver.get("http://riaz-pc.company.local:8080/clearcore")
self.login_page = page.LoginPage(self.driver)
print "I am here in setUp self.login_page = page.LoginPage(self.driver)"
self.driver.implicitly_wait(30)
def test_add_Project(self):
login_page = page.LoginPage(self.driver)
login_page.userLogin_valid()
administration_page = login_page.clickAdministration()
print "I am here administration_page = login_page.clickAdministration(self.driver)"
administration_page.AdministrationPage.add_project()
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
Pages\administrationPage.py
import time
import datetime
from selenium.common.exceptions import NoSuchElementException
from Locators.locators import MainPageLocators
from Locators import locators
from Locators import element
from Locators.element import BasePageElement
class BasePage(object):
def __init__(self, driver):
self.driver = driver
class AdministrationPage(BasePage):
# Add a project, enter project name & description, save
def add_project(self):
add_project_button = self.driver.find_element(*MainPageLocators.addButton_project)
add_project_button.click()
project_name_textfield = self.driver.find_element(*MainPageLocators.projectName_textfield)
project_name_textfield.click()
project_name_textfield.clear()
dateNow = self.get_date_now()
project_name_textfield.sendkeys('LADEMO_IE_nn_')
project_description_textfield = self.driver.find_element(*MainPageLocators.projectDescription_textfield)
project_description_textfield.click()
project_description_textfield.clear()
project_name_textfield.sendkeys("LADEMO create a basic project test script - Selenium Webdriver/Python Automated test")
Pages\page.py (This is the LoginPage class)
import time
from selenium.common.exceptions import NoSuchElementException
from Locators.element import BasePageElement
from Locators.locators import MainPageLocators
from Locators import locators
from Locators import element
from Locators.element import BasePageElement
import Locators
import Pages.administrationPage
class BasePage(object):
def __init__(self, driver):
self.driver = driver
class LoginPage(BasePage):
def click_go_button(self):
element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
element.click()
def userLogin_valid(self):
time.sleep(5)
userName_textbox = self.driver.find_element(*MainPageLocators.usernameTxtBox)
userName_textbox.clear()
userName_textbox.send_keys("user1")
password_textbox = self.driver.find_element(*MainPageLocators.passwordTxtBox)
password_textbox.clear()
password_textbox.send_keys("test123")
submitButton = self.driver.find_element(*MainPageLocators.submitButton)
submitButton.click()
#Click Administration from top menu
def clickAdministration(self):
# getting Timeout exception when i use this, using sleep for now
#WebDriverWait (self.driver, 10).until(lambda d: self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click())
time.sleep(15)
self.driver.find_element(*MainPageLocators.AdministrationButton_xpath).click()
time.sleep(5)
print "I am here, trying to click Pages.administrationPage"
return Pages.administrationPage(self.driver)
def user_login_invalid(self):
userName_textbox = self.driver.find_element(*MainPageLocators.usernameTxtBox)
userName_textbox.clear()
userName_textbox.send_keys("user1")
password_textbox = self.driver.find_element(*MainPageLocators.passwordTxtBox)
password_textbox.clear()
password_textbox.send_keys("test1")
submitButton = self.driver.find_element(*MainPageLocators.submitButton)
submitButton.click()
#check if Administration link is present. When logged in we can check are we on the correct page
def isAdministration_present(self):
#administrationLink = self.driver.find_element(*MainPageLocators.AdministrationButton_xpath)
#print "administrationLink = " + administrationLink
#wait = WebDriverWait(self.driver, 10)
#h3 = wait.until(EC.visibility_of_element_located(*MainPageLocators.AdministrationButton_xpath))
#h3.click()
#print h3.text
time.sleep(10)
try:
administrationLink_text = self.driver.find_element(*MainPageLocators.AdministrationButton_xpath)
except NoSuchElementException, e:
return False
return "Administration" in administrationLink_text.text # check if the text Administration is in the AdministrationLink_text, return true if it is
#print "administrationLink_text.text = " + administrationLink_text.text
def is_text_present(self):
try:
#body = self.driver.find_element_by_tag_name("body") # find body tag element
administrationLink_text = self.driver.find_element(*MainPageLocators.AdministrationButton_xpath)
except NoSuchElementException, e:
return False
return "Administration" in administrationLink_text.text # check if the text is in the Administration Link

Categories