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()
Related
import time
import self as self
from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from setuptools import setup
#mark.execute
class First_Tests:
def test_first(self, setup):
driver = setup['driver']
browser = setup['browser']
driver.get("https://shuftipro.com/")
driver.maximize_window()
def header_test(self, setup):
driver = setup['driver']
# Click on solution in header
solution = driver.find_element(By.ID, "menu-item-72751")
solution.click()
if driver.current_url == "https://shuftipro.com/solutions/":
print("land on solution page.")
else:
print("land on wrong page.")
obj = First_Tests()
obj.test_first(self, setup)
obj.header_test(self, setup)
If I remove the "self" from parameter and run the program it showing me error that, test_first() takes 1 positional arguments but 3 were given
one of easier way would be to use webdriver from selenium
driver = webdriver.Chrome()
and remove
driver = setup['driver']
browser = setup['browser']
and then get rid of setup as parameter.
The final code will look something like this:
from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
#mark.execute
class First_Tests:
def test_first(self):
driver.get("https://shuftipro.com/")
driver.maximize_window()
def header_test(self):
# Click on solution in header
solution = driver.find_element(By.ID, "menu-item-72751")
solution.click()
if driver.current_url == "https://shuftipro.com/solutions/":
print("land on solution page.")
else:
print("land on wrong page.")
obj = First_Tests()
obj.test_first()
obj.header_test()
I tried to access a website and find its element. The result says:
AttributeError: type object 'By' has no attribute 'id'.
import unittest from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
class test_case(unittest.TestCase):
def testName(self):
driver = webdriver.Chrome(options=chrome_options)
driver.get("bing.com/?rtc=1")
Element = driver.find_element(By.ID, "container")
self.assertIsNone(Element, "ID element is not none") # (to be continued)
if name == 'main':
unittest.main()
I want to use the assertIsNone() but in console, the result says it ran but has Errors=1.
I want to display a message using the syntax assertIsNone but it did not occur on the console. What should I do about the error?
Is that what you need?:
import unittest from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
class test_case(unittest.TestCase):
def testName(self):
driver = webdriver.Chrome(options=chrome_options)
driver.get("bing.com/?rtc=1")
try:
Element = driver.find_element(By.ID, "container")
success = True
except:
success = False
self.assert(success, True) # (to be continued)
if name == 'main':
unittest.main()
If not, feel free to tell me the issue, and I'll edit my answer.
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()
I have a problem the browser manager library does not work Webdriver Manager for Python. The following error appears, tell me how to fix it.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.firefox import GeckoDriverManager
class MainPage:
driver = webdriver.Chrome(GeckoDriverManager().install())
def __init__(self, link):
self.link = link
def connect(self):
with self.driver as dr:
dr.get(self.link)
if __name__ == '__main__':
m = MainPage('https://ru.wikipedia.org/')
ValueError:
Could not get version for Chrome with this command: reg query
"HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version
GeckoDriverManager - this should be for FireFox.
Why are you mixing things ?
Chrome(GeckoDriverManager().install())
This is not right way.
Install webdriver manager like this :
pip3 install webdrivermanager
and instantiate chrome driver like this :
driver = webdriver.Chrome(ChromeDriverManager().install())
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)