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.

1

2 Answers 2

6

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')]")
Sign up to request clarification or add additional context in comments.

5 Comments

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 [].
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)
a live url to your page also help
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.
Inspect will see rendered html (may result from javascript renderer) give url so we can help you select correct data
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?

2 Comments

None of these works with error: unable to locate element.
@kelvin Check out my answer update and let me know the status.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.