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!

Community
  • 1
  • 1
Onur
  • 19
  • 6
  • Have you tried giving the link an id and selecting by id? Also, is selenium waiting until the page DOM has loaded? – raduation Aug 24 '15 at 08:33
  • Yes, I tried giving id too. I am reaching the page when it stops working, so I guees Selenium loads it properly. – Onur Aug 24 '15 at 08:40
  • What I mean is, you may need to tell Selenium to wait until the element is present. Take a look at http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits – raduation Aug 24 '15 at 08: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. – NoSuchElephantException Aug 24 '15 at 09:04
  • @Onur `driver.find_elements_by_xpath("//*[contains(text(), 'Print')]").click()` – dsgdfg Aug 24 '15 at 09:06

0 Answers0