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– FDM2016年02月17日 07:15:08 +00:00Commented Feb 17, 2016 at 7:15
-
yes I am 100% sure about the id and click functionalityQAMember– QAMember2016年02月17日 08:13:49 +00:00Commented Feb 17, 2016 at 8:13
4 Answers 4
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– kumar2017年08月31日 19:38:15 +00:00Commented Aug 31, 2017 at 19:38
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. ;)
"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 seleniumQAMember– QAMember2016年02月17日 08:13:02 +00:00Commented Feb 17, 2016 at 8:13
-
This shouldn't change for manual clicking and automated using Selenium. Is the JS function performing correctly?demouser123– demouser1232016年02月17日 08:17:08 +00:00Commented Feb 17, 2016 at 8:17
-
Is it similar to sqa.stackexchange.com/questions/13314/…. You asked this questiondemouser123– demouser1232016年02月17日 08:18:32 +00:00Commented 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 helpQAMember– QAMember2016年02月18日 05:09:11 +00:00Commented Feb 18, 2016 at 5:09
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.