3

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?

asked Feb 17, 2016 at 7:02
2
  • Are you sure the locator is correct and the click actually occurs? Commented Feb 17, 2016 at 7:15
  • yes I am 100% sure about the id and click functionality Commented Feb 17, 2016 at 8:13

4 Answers 4

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();
answered Feb 18, 2016 at 7:25
1
  • This is working perfectly. Commented Aug 31, 2017 at 19:38
1

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. ;)

answered Feb 17, 2016 at 8:19
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.

answered Feb 17, 2016 at 7:31
4
  • 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 Commented Feb 17, 2016 at 8:13
  • This shouldn't change for manual clicking and automated using Selenium. Is the JS function performing correctly? Commented Feb 17, 2016 at 8:17
  • Is it similar to sqa.stackexchange.com/questions/13314/…. You asked this question Commented Feb 17, 2016 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 Commented Feb 18, 2016 at 5:09
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.

answered Dec 24, 2016 at 5:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.