question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Click javascript events not firing? dispatchEvent seems to not work

See original GitHub issue

The javascript executor does not seem to launch the click event. But when I run the same javascript directly via dev tools it opens the new page correctly.

I would expect the “test” search results to show, but they do not.

I tried several approaches without success and started to wonder if its a bug or a limitation with HtmlUnit or the driver not correctly processing the javascript.

Not sure if I should open the issue in or in the webdriver repo, can re-open if it is not in the correct location.

package seleniumHtmlunit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.javascript.SilentJavaScriptErrorListener;

public class Test {

	public static void main(String[] args) {

		WebDriver webDriver = new HtmlUnitDriver(BrowserVersion.FIREFOX, true) {
		    @Override
		    protected WebClient modifyWebClient(WebClient client) {
		        final WebClient webClient = super.modifyWebClient(client);
		        webClient.getOptions().setCssEnabled(false);
		        webClient.getOptions().setJavaScriptEnabled(true);
		        webClient.getOptions().setDownloadImages(false);
		        webClient.getOptions().setThrowExceptionOnScriptError(false);
		        webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());

		       return webClient;
		    }
		};

		duckDuckTest(webDriver);
		webDriver.close();
		
	}

	private static void duckDuckTest(WebDriver webDriver) {
		webDriver.get("https://duckduckgo.com/");
		JavascriptExecutor js = (JavascriptExecutor) webDriver;
		String script = "var search = document.evaluate(\"//input[@id='search_form_input_homepage']\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\r\n"
				+ "search.value=\"test\";\r\n"
				+ "\r\n"
				+ "const event = new MouseEvent('click', {\r\n"
				+ "    view: window,\r\n"
				+ "    bubbles: true,\r\n"
				+ "    cancelable: true\r\n"
				+ "  });\r\n"
				+ "var cb = document.evaluate(\"//input[@id='search_button_homepage']\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;\r\n"
				+ "\r\n"
				+ "cb.dispatchEvent(event)";
		js.executeScript(script);

		
	}

}

This is the javascript:

var search = document.evaluate("//input[@id='search_form_input_homepage']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
search.value="test";

const event = new MouseEvent('click', {
    view: window,
    bubbles: true,
    cancelable: true
  });
var cb = document.evaluate("//input[@id='search_button_homepage']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

cb.dispatchEvent(event)

You can wait for a condition such as

(new WebDriverWait(webDriver,30)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[normalize-space()='The name test is reserved by the Internet Engineering Task Force in RFC 2606 as a domain name that is not intended to be installed as a top-level domain in the global Domain Name System of the Internet for production use.']")));

and it fails. It never reaches the next page, clicking the search button by firing off the click event.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rbricommented, Nov 3, 2020

Hi @skyhirider, made the first commits for this problem. If the build is green, i will build a new snapshot. Please check at twitter.

Some background: The problem here is the line

event = new MouseEvent('click', 

The first parameter was ignored and as a result the event dispatcher has not forced the click handling. So far i have done a bunch of fixes for the MouseEvent impl but i have to fix all the other event classes also.

Will be great if you can test the new snapshot (as soon as it is available 😃 and please provide feedback.

1reaction
rbricommented, Nov 2, 2020

Ok, think a found at least one reason for this - will require some days to fix because i fear i have to write a bunch of tests for all our event implementations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS dispatchEvent not working - Stack Overflow
1 Answer 1 · Right click on the attach element. · Select inspect element in chrome browser · In the right panel select...
Read more >
Event listeners not working? 3 key areas to troubleshoot
Are your event listeners not working as you'd expect? Here are 3 key areas to troubleshoot to help you get everything triggering as...
Read more >
EventTarget.dispatchEvent() - Web APIs | MDN
Calling dispatchEvent() is the last step to firing an event. The event should have already been created and initialized using an Event() ...
Read more >
JavaScript dispatchEvent(): Generate Events programmatically
In this tutorial, you'll learn how to programmatically create and dispatch events using Event constructor and dispatchEvent() method.
Read more >
Event Dispatch Event not Firing on Parent LWC
I presume that your template is incorrect. The event handler should be bound as: <c-child onselected-surveys={selectedSurveys}>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found