IndieDev
IndieDev

Reputation: 175

Using javascriptExecutor to find a WebElement

The title says all, I need a way to select a WebElement.

I am using Firefox Quantum (latest beta version).

This is what I tried.

String ccNumberSelector = "'#card_details > div > input'";
                            Object ccNumberField = js.executeScript("return $(" + ccNumberSelector + ").eq(0);");
                            ((WebElement) ccNumberField).sendKeys("some key sequence"); 

All that this does is 'freeze' (not really freeze, it just blocks the whole webpage, like you can't interact with it anymore).

What am I doing wrong?

PS: I CANNOT use any other selector than this (id's and everything else are random).

Upvotes: 0

Views: 1787

Answers (1)

Fenio
Fenio

Reputation: 3625

Use

js.executeScript("return document.querySelector(" + ccNumberSelector + ");

Warning: The argument for querySelector must be a CSS Selector, not XPath.

Upvotes: 1

Related Questions