0
votes

I have EXACTLY (character, spacing indentation) what the instructor has in the Udemy tutorial. His test is running fine but mine is getting errors. Please review below. Thanks!

import unittest
from selenium import webdriver


class MyTestCase(unittest.TestCase):

    def setup(self):
        self.driver = webdriver.Chrome(executable_path="../Drivers/chromedriver.exe")
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()

    def test_search(self):
        self.driver.get("http://www.google.com")
        self.driver.find_element_by_name("q").send_keys("Automation step by step")
        self.driver.find_element_by_name("btnk").click()
        x = self.driver.title
        print(x)
        self.assertEqual(x, "Automation step by step")

    def tearDown(self):
        self.driver.close()
        self.driver.quit()

Error Traceback (most recent call last): File "C:\Python27\Lib\unittest\case.py", line 329, in run testMethod()

File "C:\Users\S-Iran\PycharmProjects\Demo\Demo_1\utest1.py", line 15, in test_search self.driver.find_element_by_name("btnk").click()

File "C:\Users\S-Iran\PycharmProjects\Demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name return self.find_element(by=By.NAME, value=name)

File "C:\Users\S-Iran\PycharmProjects\Demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value']

File "C:\Users\S-Iran\PycharmProjects\Demo\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response)

File "C:\Users\S-Iran\PycharmProjects\Demo\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace)

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"btnk"} (Session info: chrome=72.0.3626.121) (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)

1
Your "setup" function should be called "setUp" in order to be invoke first - Kipi
Thank you so much! Did you notice that from the errors? Or Just off reading the method? Now I have different errors. I inspected the elements on Google and they still have the correct names so I'm not sure why they can't be found. NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"btnk"} - S-Iran
Please update your question - Kipi
Question updated. - S-Iran
In your html there is no element named "btnk", it is pretty straight forward - Kipi

1 Answers

1
votes

Instead of click on button which is not present when you do sendKeys you can use element.submit()

Instead of this.

self.driver.get("http://www.google.com")
  self.driver.find_element_by_name("q").send_keys("Automation step by step")
  self.driver.find_element_by_name("btnk").click()

Try this.

element=self.driver.find_element_by_name("q")
element.send_keys("Automation step by step")
element.submit()