0
votes

Code is following like this one

from selenium import webdriver
import unittest
import HtmlTestRunner
from selenium.webdriver.common.by import By

class TestReport(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(executable_path="F:\\3-2 Projects\\PycharmProjects\\Selenium01\\drivers\\chromedriver.exe")
        cls.driver.implicitly_wait(2)

    def test_Case_1(self):
        self.driver.get("https://opensource-demo.orangehrmlive.com/")
        self.driver.find_element(By.LINK_TEXT,"Forget your password?").click()

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()
        print("Test Completed")

if name == 'main': unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='F:/3-2 Projects/PycharmProjects/Selenium01/Reports'))



Give some response












Output:

Testing started at 10:14 PM ... C:\Users\User\AppData\Local\Programs\Python\Python38\python.exe "C:\Program Files\JetBrains\PyCharm 2019.2.6\helpers\pycharm_jb_unittest_runner.py" --path "F:/3-2 Projects/PycharmProjects/Selenium01/ReportSelenium.py" Launching unittests with arguments python -m unittest F:/3-2 Projects/PycharmProjects/Selenium01/ReportSelenium.py in F:\3-2 Projects\PycharmProjects\Selenium01

Error Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 60, in testPartExecutor yield File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 676, in run self._callTestMethod(testMethod) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 633, in _callTestMethod method() File "F:\3-2 Projects\PycharmProjects\Selenium01\ReportSelenium.py", line 16, in test_Case_1 self.driver.find_element(By.LINK_TEXT,"Forget your password?").click() File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Forget your password?"} (Session info: chrome=89.0.4389.90)

Assertion failed

Test Completed

Ran 1 test in 14.704s

FAILED (errors=1)

Process finished with exit code 1

Assertion failed

Assertion failed

1

1 Answers

0
votes

Seems like element has not been found by provided selector, try to change it on the next one:

//div[@id='forgotPasswordLink']/a

so, the step of finding element looks like:

self.driver.find_element(By.XPATH,"//div[@id='forgotPasswordLink']/a").click()

or

self.driver.find_element(By.ID,"forgotPasswordLink").click()

Couple of tricky things:

1.Install webdriver-manager, it allows you install and store chromedrive automatically

pip install webdriver-manager

and use like this:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

2.Starting using WebdriverWait, allows waiting element dynamically and get it at once:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
timeout = 30

element = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, 'forgotPasswordLink')))
element.click()