Separate driver file for browser selenium python base - python

I tried to have a separate file for browser driver, in my driver file with a file name driver.py:
from selenium import webdriver
class MyDriver():
def __init__(self):
self.driver = webdriver.Firefox()
I try reusing it on another file here's the code:
from driver import MyDriver
driver = MyDriver()
driver.get('http://google.com')
However, I always got an error:
AttributeError: 'MyDriver' object has no attribute 'get'

You simply forgot to add another driver. Try this:
driver.driver.get('http://google.com')

Related

I am getting the exception while running below code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.chrome(executable_path="C:\Driver\chromedriver_win32\chromedriver.exe")
driver.get("www.youtube.com")
print (driver.title)
driver.close()
for the above code I'm getting the error
TypeError: 'module' object is not callable
I recommend modular programming.
Set a function that returns the chrome driver like such:
# Opens chrome driver
def openChrome():
# directory to chromedrive
chromeDriver = "C:\Driver\chromedriver_win32\chromedriver.exe"
return webdriver.Chrome(chromeDriver)
Call this function like this:
url = "https://stackoverflow.com/"
driver = openChrome()
driver.get(url)
So in the future if your driver isn't working, you don't have to change it on every script you will change it in one place (function)

Selenium cannot enable javascript in python

how do I enable javascript in selenium using python? I tried several methods but mine isnt working. Does anyone know how could i fix this issue? Thanks
My code
from selenium import webdriver
import urllib
import urllib.request
import string
from bs4 import BeautifulSoup
import mysql.connector
import time
chrome_path = r"C:\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.add_argument("--enable-javascript")
Error i got
Exception has occurred: AttributeError
'WebDriver' object has no attribute 'add_argument'
File "C:\Users\bustillo-ronald\Desktop\python-basics\Scrape\propertyguru.py", line 11, in <module>
driver.add_argument("--enable-javascript")
You need to create an instance of a ChromeOptions object first and add_argument to that. Then pass the options object as an argument to the Chrome webdriver.
something like this
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
# Now back to your code
driver = webdriver.Chrome(chrome_path, options=options)
...

TypeError: get() missing 1 required positional argument: 'url' error using GeckoDriver and Firefox through Selenium and Python

Executing below code in pycharm.
from selenium import webdriver
browser = webdriver.Firefox
browser.get('https://www.google.com')
Error:
TypeError: get() missing 1 required positional argument: 'url'
How can I solve the error?
Specify the path the chrome driver is located in for example when calling
webdriver.Firefox(‘C://Users/Username/Downloads/‘)
This worked for me:
from selenium import webdriver
driver = webdriver.Chrome("C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
Alternate code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Users\Rishabh\Downloads\chromedriver_win32\chromedriver.exe")
driver.get('https://web.whatsapp.com/')
In my case, I got this error for not using parenthesis ().
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.google.com')
Try with braces while creating Firefox instance. see below example.
from selenium import webdriver
browser = webdriver.Firefox() #focus on () at the end
browser.get('https://www.google.com')
The constructor is driver = webdriver.Firefox(). So in your code block you need to replace driver = webdriver.Firefox with:
driver = webdriver.Firefox()
Additionally, you may need to pass the absolute path of the GeckoDriver binary as follows:
driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')
The issue is caused bacause there are not,()pharentesis, put the pharantesis end to the line
Check this
In Selenium and python
driver = webdriver.Chrome()

Python Selenium - Using same driver instance which is initiated in another file

In first file , there is a below code.
I want to use the driver instance of first file in second file , I am able to call it but getting an exception Nosuchelementexception
Basically i want the same browser session in both files , note that import statements are provided properly to use those.
class Init():
driver = webdriver.Chrome(
executable_path="C:\Program Files (x86)\Python36-32\selenium\webdriver\chromedriver_win32\chromedriver.exe")
def take_screenshot(self):
Init.driver.get_screenshot_as_png("Testcase.png")
def browser_launch(self):
Init.driver.set_page_load_timeout(20)
Init.driver.get("http://url/")
Init.driver.maximize_window()
def user_comes_in(self):
Init.driver.find_element_by_id("username").send_keys("admin")
Init.driver.find_element_by_name("password").send_keys("admin")
Init.driver.find_element_by_class_name("Button").click()
Init.driver.set_page_load_timeout(20)
In second file , here is the code
initiate = Init()
class Two(unittest.TestCase):
initiate.browser_launch()
def test_user_logs(self):
initiate.user_comes_in()
print("test case one")
def test_user_create(self):
initiate.user_creation()
print("Test case two")
if you can keep the browser open, you can do it like this:
init.py:
def setDriver():
driver = webdriver.Firefox()
driver.maximize_window()
driver = setDriver()
1.py:
from init.py import driver
driver.get('xxxx')
2.py:
from init.py import driver
driver.get('yyyy')
they will use same driver and same browser.
but if you close the driver in any of the case file, others can't use it again. so it only available in cases don't need to close browser.

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