Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium: Click a href="javascript:()"

I am using chromedriver and I have the following webpage source:

<form id="stepLinksForm" name="stepLinksForm" method="POST" target="mainFrame"> 

  <ul> 
      <li> <a href="javascript:submitLink('action_one.htm')">Action One</a> </li>
      <li> <a href="javascript:submitLink('action_two.htm')">Action Two</a> </li>
      <li> <a href="javascript:submitLink('action_three.htm')">Action Three</a> </li>
  </ul>   

</form>

After clicking anyone of the href, the browser goes to a new page but the url stays the same. What I want to achieve is clicking the first href, i.e. <li> <a href="javascript:submitLink('action_one.htm')">Action One</a> </li>

I have tried find_element_by_xpath, link_text and some other methods suggested on the Internet but none of them works. I really appreciate if someone could help.

like image 449
kelvin Avatar asked Feb 12 '26 18:02

kelvin


1 Answers

Instead of click you can call the javascript code directly:

browser.execute_script("submitLink('action_one.htm')")

which equivalent to javascript:submitLink('action_one.htm')

Or you can find the a by its text:

browser.find_elements_by_xpath("//a[contains(text(), 'Action One')]")
like image 136
James Avatar answered Feb 15 '26 06:02

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!