0

When trying to use this format of selector with SeleniumJavascriptExecutor:

js.executeScript("arguments[0].textContent=${value}", driver.findElement(By.cssSelector('div.stb-LazyChosenDropdown div.tiles div.input:nth-child(1)')))

The following error is thrown:

org.openqa.selenium.JavascriptException: javascript error: Invalid or unexpected token

Yet it works without the :nth-child(1) part just fine so I'm inclined to think it doesn't like the (1) in the string which my IDE seems to think is an integer

Steerpike
  • 1,712
  • 6
  • 38
  • 71

3 Answers3

0

Try with nth-of-type(1) instead of nth-child(1)

js.executeScript("arguments[0].textContent=${value}", driver.findElement(By.cssSelector('div.stb-LazyChosenDropdown div.tiles div.input:nth-of-type(1)')))
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • this doesn't work unfortunately. The selector I used is fine I think, it's just Javascript Executor doesn't like the (1) token in the selector – Steerpike Feb 24 '23 at 11:18
0

This cssSelector:

input:nth-child(1) 

selects the <input> element that is the first child of its parent can be replaced with:

input:first-child

which will also select the <input> element that is the first child of its parent.

Effectively the locator strategy will be:

driver.findElement(By.cssSelector('div.stb-LazyChosenDropdown div.tiles div.input:input:first-child'))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I can't tell what language that is but it should probably look like this:

js.executeScript("arguments[0].innerText = arguments[1]", element, value)
pguardiario
  • 53,827
  • 19
  • 119
  • 159