0

I am writing Selenium tests for my applications and for my own use. Every time I tried to reach Javascript functions or click buttons in the source code, I got errors. Here is the basic example:

            <font class="bodytextdark"></font>
            <br></br>
            <br></br>
            <table width="90%" cellspacing="0" cellpadding="3" bordercolor="#111111" border="1" style="border-collapse: collapse"></table>
            <br></br>
            <font color="red"></font>
            <form action="stuinfgpacalc.asp" method="POST" name="gpaform">
                <p>
                    <a href="Javascript:window.close()">

                    Close

                    </a>
                    <a href="Javascript:window.print()">

                    Print

                    </a>
                </p>
                <table width="90%" cellspacing="0" cellpadding="3" bordercolor="#111111" border="1" style="border-collapse: collapse"></table>
            </form>

I tried to click Print button via code stated below:

def setUp(self):
    self.selenium = webdriver.Firefox()
    self.selenium.maximize_window()

def test_get_gpa_calc_result(self):
    ........

    """
    Print the form.
    """
    self.selenium.find_element_by_xpath('//a[@href= "Javascript:window.print()"]').click()

I got the error: NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//a[@href= \"Javascript:window.print()\"]"} Stacktrace

I also tried to reach button via "Print" name too, but it doesn't work.

I checked every solution in here like that one: Clicking JavaScript links in Selenium WebDriver and Python ,but it doesn't work for me. Any suggestions are more than welcomed!

5
  • Have you tried giving the link an id and selecting by id? Also, is selenium waiting until the page DOM has loaded? Commented Aug 24, 2015 at 8:33
  • Yes, I tried giving id too. I am reaching the page when it stops working, so I guees Selenium loads it properly. Commented Aug 24, 2015 at 8:40
  • What I mean is, you may need to tell Selenium to wait until the element is present. Take a look at docs.seleniumhq.org/docs/… Commented Aug 24, 2015 at 8:42
  • Can you share the full code for test_get_gpa_calc_result? There is likely a step or missing step there which is causing your NoSuchElementException, since the xpath is correct for the HTML sample you provided. Like raduation said, you may need to add a wait command. Commented Aug 24, 2015 at 9:04
  • @Onur driver.find_elements_by_xpath("//*[contains(text(), 'Print')]").click() Commented Aug 24, 2015 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.