Trying to setup Chrome for the open / close a website. Now i can open it, But failed to close it.
Can anyone tell me why? Many thanks!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class chromeSetup():
def __init__(self):
self.chrome_path = r'C:\XXXXX\chromedriver.exe'
def searchWeb(self, url="https://www.google.com.hk/"):
driver = webdriver.Chrome(self.chrome_path)
driver.get(url)
def close(self):
self.driver.close()
You are not making driver an instance attribute. Change searchWeb method like this:
def searchWeb(self, url="https://www.google.com.hk/"):
self.driver = webdriver.Chrome(self.chrome_path)
self.driver.get(url)
Related
I am learning selenium in python with pytest, I am facing this below error. I have searched this error all over the internet and tried all the possible advice, but nothing is working.
I am just trying to load the website, it is opening the browser but failing with this error. I don't know what i am missing, Any lead would be helpful.
FAILED Tests/test_webtable.py::test_webtablepage - AttributeError: type object 'WebTablePage' has no attribute 'load'
Code
Page Object Class(webtablepage.py) under pages folder.
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class WebTablePage:
#URL
URL = 'https://demoqa.com/webtables'
#Intializers
def __init__(self,browser):
browser = self.browser
def load(self,browser):
self.browser.get(self.URL)
Pytest fixture(conftest.py) under Tests folder.
import pytest
import selenium.webdriver
#pytest.fixture
def browser():
#initialize the chrome instance
driver = selenium.webdriver.Chrome()
#Making the Driver wait for 10 seconds to load elements
driver.implicitly_wait(10)
#Return the webdriver instances for the setup
yield driver
#Quit the webdriver instances for the cleanup
driver.quit()
Test function (test_webtable.py) under Tests folder
from Pages.webtablepage import WebTablePage
def test_webtablepage(browser):
Webtable_page = WebTablePage
# Given the demoa qa Webtables page
Webtable_page.load()
The WebTablePage constructor is called incorrectly. It must be called with the browser argument.
Change this:
def test_webtablepage(browser):
Webtable_page = WebTablePage
To this:
def test_webtablepage(browser):
Webtable_page = WebTablePage(browser)
Change the
Webtable_page = WebTablePage
to
Webtable_page = WebTablePage()
I've been looking for a solution to inherit the methods of the python selenium webdriver class so that I can extend/modify its behaviour. I have not found one that work.
For instance, I want to inherit all the methods of the chrome webdriver but extend the .get() method (i.e. the method to load a url). This is my current approach:
from selenium import webdriver
from selenium.common.exception import TimeoutException
class CustomDriver:
def __init__(self, browser):
if browser.lower() == 'chrome'
self.driver = webdriver.Chrome()
def get_url(self, url):
try:
self.driver.get(url)
except TimeoutException:
self.driver.quit()
This methods works but it does not inherit the general webdriver methods like driver.quit(). In fact, if I do the following:
mydriver = CustomDriver('Chrome')
mydriver.quit()
I get the error: 'Custom driver' object has no attribute quit.
Has any of you any suggestions?
ps: I'm new to python.
quit() is from webdriver methods and you are importing it from selenium module, and you have created a object self.driver for the webdriver.Chrome()
so you can't use webdriver method on created class object that is the reason why you are getting No Attribute Error : 'Custom driver' object has no attribute quit
One way of obtaining inheritance is by declaring respective method inside the class and inherit those methods outside.
Refer this document: https://www.geeksforgeeks.org/web-driver-methods-in-selenium-python/
Below is the code that would work
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
class CustomDriver:
def __init__(self, browser):
if browser.lower() == 'chrome':
self.driver = webdriver.Chrome()
def exit_browser(self):
self.driver.quit()
def get_url(self, url):
try:
self.driver.get(url)
except TimeoutException:
self.driver.quit()
mydriver = CustomDriver('Chrome')
mydriver.get_url("https://www.google.com/")
mydriver.exit_browser()
I am trying to learn python and selenium. and I am using pytest-bdd. and there is conftest.py. Normally I defined my driver in this page under before_scenarios method. And I have basepage which contains click,hover_to,text_presents etc some operations. Conftest also use my BasePage click method to close popups. and I have LoginTest.py which use Basepage.
How can I use driver for all pages. because I cant get driver. What is my mistake also could u pls explain?
Here is my code:
conftest.py:
bp = BasePage.BasePage()
def pytest_bdd_before_scenario():
global driver
url="myurl"
logger.info("\nBrowser operations before test running\n")
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
website_is_up_and_running(url)
bp.click((By.XPATH , "//div[#title='Kapat']"))
bp.click((By.XPATH , "//a[#class='close-button']"))
Here is my BasePage:
class BasePage():
def click(self,driver,by_locator):
WebDriverWait(driver, 60) .until(EC.visibility_of_element_located((by_locator))).click()
And Finally, this is my LoginTest.py:
import utilities.BasePage as BasePage
bp = BasePage.BasePage()
#when('I click the Login Button')
def i_click_the_login_button():
bp.hover_to_and_click(driver,(By.XPATH , "(//p[#class='link-text'])[1]")) //in this line I can't get driver.
I'm working on an automation script and I'm trying to open a url in chrome, I have installed and imported selenium and downloaded the chrome driver and moved it to /usr/local/bin.
But when I try and run the script, the console is blank and then about a second later it displays 'Process finished with exit code 0' as if nothing happened. Below is my current code:
from selenium import webdriver
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')
The path is wrong here. It should be like below
webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver.exe')
Need to add .exe extension in the executable_path.
Also, make sure you are using correct Version of chrome driver for Google Chrome.
You are on linux system. You don't need the raw i.e. r switch. Your effective line of code will be:
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
Does this work?
from config import keys
from selenium import webdriver
def order():
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get('https://facebook.com')
print("here")
if __name__== '__main__':
order()
An exit code 0 means that ran without error. If an error occurs it would provide a non-zero argument. I would add a
from selenium import webdriver
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')
print ('Opened facebook')
This should return with "Opened facebook" then 'Process finished with exit code 0'. I built something similar that will log a user into Facebook.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')
print ('Opened facebook')
self.driver.implicitly_wait(30)
self.driver.get(k['product_url'])
print ('Opened facebook')
username_box = self.driver.find_element_by_id('email')
username_box.send_keys('EMAIL ADDRESS')
print ('Email Id entered')
password_box = self.driver.find_element_by_id('pass')
password_box.send_keys('password')
print ('Password entered')
login_box = self.driver.find_element_by_id('loginbutton')
login_box.click()
print('Logged In')
I am trying to automate a web file downloading program with Selenium in Python. But I have some difficulties in clicking one particular button with Selenium: The program succeeds in leading to this url 'https://www.sec.gov/Archives/edgar/data/1467373/000119312510235847/0001193125-10-235847-index.htm', but it cannot click on the button of the first document (d10k.htm). The button is defined as 'formbuttonElement' in the following code and I tracked it by Xpath. In addition, I used both click() and .send_keys(Keys.SPACE) methods, but they didn't work.
Can someone have a look at this problem?
Thank you!
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
import os
class LoginTest(unittest.TestCase):
def setUp(self):
fp=webdriver.FirefoxProfile(r"C:\Users\sxc\AppData\Roaming\Mozilla\Firefox\Profiles\x5i7u4m7.profileToolsQA")
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "D:\doc1")
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
self.driver=webdriver.Firefox(firefox_profile=fp)
self.driver.get("https://www.sec.gov/edgar/searchedgar/companysearch.html")
def test_Login(self):
driver=self.driver
cikID="cik"
searchButtonID="cik_find"
typeID="//*[#id='type']"
priorID="prior_to"
cik="00001467373"
Type="10-K"
prior="20101231"
search2button="//*[#id='contentDiv']/div[2]/form/table/tbody/tr/td[6]/input[1]"
documentsbuttonid="documentsbutton"
formbuttonxpath="(//a[contains(#href,'/Archives/edgar/data/')])[1]"
cikElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(cikID))
cikElement.clear()
cikElement.send_keys(cik)
searchButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(searchButtonID))
searchButtonElement.click()
typeElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(typeID))
typeElement.clear()
typeElement.send_keys(Type)
priorElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_id(priorID))
priorElement.clear()
priorElement.send_keys(prior)
search2Element=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(search2button))
search2Element.send_keys(Keys.SPACE)
time.sleep(1)
documentsButtonElement=WebDriverWait(driver,20).until(lambda driver:driver.find_element_by_id(documentsbuttonid))
documentsButtonElement.click()
formElement=WebDriverWait(driver,30).until(lambda driver:driver.find_element_by_xpath(formbuttonxpath))
formElement.send_keys(Keys.SPACE)
def terdown(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
Try this line of code
driver.find_element_by_xpath('//a[text()="d10k.htm"]').click()