2

I have an requirement of clicking on a link . I am using click() method to click on link by providing the locators and etc.., after click no action was taken place. When I see the html code anchor tag href attribute is having

"javascript:void(0)"

So there is no action taken place.The id of anchor tag is binded with java script and from there it is submitting the request.

How can I handle this scenario from selenium?

  • Are you sure the locator is correct and the click actually occurs? – FDM Feb 17 '16 at 7:15
  • yes I am 100% sure about the id and click functionality – QAMember Feb 17 '16 at 8:13
3

In my code, Anchor tag is having the href attribute as below

"javascript:void(0)

but when we click on anchor tag form was submitting though javascript. So in this case when click() to click on link will not work. we should use the submit().

Finally this issue is resolved with following code

driver.findElement(By.xpath(xpath)).submit();
  • This is working perfectly. – kumar Aug 31 '17 at 19:38
0

"javascript:void(0)" means that the link wouldn't work. It'll do nothing. That is why no action is taking place when you click on it.

I have seen many developers do this when they don't want the link to "navigate" anywhere, though it's not the right practice.

You can read about this here.

  • Hello @log_file,yes you are correct but when I manually clicking on the link,some action was taken place and redirected to another page.I am asking the same how we can do it using selenium – QAMember Feb 17 '16 at 8:13
  • This shouldn't change for manual clicking and automated using Selenium. Is the JS function performing correctly? – demouser123 Feb 17 '16 at 8:17
  • Is it similar to sqa.stackexchange.com/questions/13314/…. You asked this question – demouser123 Feb 17 '16 at 8:18
  • yes you are correct.Both are same issues but in different scenario's. I got the solution also.Any way Thanks for you help – QAMember Feb 18 '16 at 5:09
0

You can try triggering the Javascript event by using the Javascript native click:

WebElement el = webDriver.findElement(...);
JavascriptExecutor exec = (JavascriptExecutor) webDriver;
exec.executeScript("arguments[0].click()", el);

Although I'm still convinced that a Selenium click will trigger Javascript code. ;)

0

This worked for me after spending hours fighting with javascript:void(0)

import os
import time
from selenium import webdriver
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome()

driver.get('https://www.website.com')
print (driver.title)

elem = driver.find_element_by_css_selector("something-here")
important_is_needed = elem.location_once_scrolled_into_view
elem.click()
time.sleep(1)

See this for Linux chrome install for selenium https://stackoverflow.com/questions/8255929/running-webdriver-chrome-with-selenium

Note this comment "you can install chromium-chromdriver package (e.g. with apt-get) and add the path of the chromedriver file to the PATH shell variable PATH="${PATH}":/usr/lib/chromium-browser/ If you add this to your .bashrc, you won't need to set it each time you want to test with selenium" Thanks @Arpad Horvath

My install commands.

sudo pip install pyvirtualdisplay

sudo apt-get install xvfb

sudo apt-get install chromium-chromedriver

Also note the selenium ide firefox plugin can help. Recording with the ide will tell you the correct string to use for find_element_by_css_selector. Also, if this is new to you, firefox developer inspector tool and chrome inspect element tool are invaluable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.