Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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!

share|improve this question
    
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 8: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 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/… – raduation Aug 24 '15 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. – NoSuchElephantException Aug 24 '15 at 9:04
    
@Onur driver.find_elements_by_xpath("//*[contains(text(), 'Print')]").click() – dsgdfg Aug 24 '15 at 9:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.