2

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.

JacobIRR
  • 8,545
  • 8
  • 39
  • 68
kelvin
  • 43
  • 1
  • 1
  • 5
  • does this answer help? https://stackoverflow.com/questions/24312979/selenium-and-clicking-an-a-with-href-javascript – JacobIRR Mar 30 '18 at 19:21

2 Answers2

5

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')]")
James
  • 13,571
  • 6
  • 61
  • 83
  • Thanks for your response. I tried the first method but returns "WebDriverException: Message: unknown error: submitLink is not defined". I tried the second one before with .click() but it returns 'list' object has no attribute 'click' and I print what is found it reutrn an empty list []. – kelvin Mar 30 '18 at 19:51
  • That's look like your page is render with javascript and or it live inside a container (iframe). Please try to wait for content render (sleep) before execute code. Also try to view source of the page (not dev tool view source) – James Mar 30 '18 at 19:54
  • a live url to your page also help – James Mar 30 '18 at 19:55
  • I put a time.sleep but still nothing changed. I cannot see the web source I want by "View Page Source" but I see them by "Inspect". Does this imply anything? And how can a live url helps? Thanks. – kelvin Mar 30 '18 at 20:17
  • Inspect will see rendered html (may result from javascript renderer) give url so we can help you select correct data – James Mar 31 '18 at 00:28
3

To click on the first href with text as Action One you can use either of the following options (Python Language Binding Art) :

  • linkText :

    driver.find_element_by_link_text("Action One").click()
    
  • cssSelector :

    driver.find_element_by_css_selector("a[href*='action_one.htm']").click()
    
  • xpath :

    driver.find_element_by_xpath("//a[contains(@href,'action_one.htm') and contains(.,'Action One')]").click()
    

Update

As you are unable to locate the element through LINK_TEXT, CSS, XPATH and even after time.sleep() it is pretty much confirmed that the element is within an frame which is denoted by the <frame> tag.

Now as you are able to see them by "Inspect", locate the element within the HTML DOM and traverse up the HTML. At some point you will find a <frame> tag. Grab the attributes of the <frame> tag and switch to the intended frame first and then try to use the Locator Strategies provided in my answer. Here you can find a detailed discussion on How can I select a html element no matter what frame it is in in selenium?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352