Python Selenium: Click a href="javascript:()"
Solution 1
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')]")
Solution 2
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?
kelvin
Updated on June 13, 2022Comments
-
kelvin almost 2 years
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 about 6 yearsdoes this answer help? stackoverflow.com/questions/24312979/…
-
JeffC about 6 yearsSee: How do I do X? The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: How to Ask
-
-
kelvin about 6 yearsThanks 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 [].
-
James about 6 yearsThat'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 about 6 yearsa live url to your page also help
-
kelvin about 6 yearsNone of these works with error: unable to locate element.
-
kelvin about 6 yearsI 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.
-
undetected Selenium about 6 years@kelvin Check out my answer update and let me know the status.
-
James about 6 yearsInspect will see rendered html (may result from javascript renderer) give url so we can help you select correct data