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!