Skip to content

Click javascript events not firing? dispatchEvent seems to not work #259

@skyhirider

Description

@skyhirider

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions