Can't execute focus() with JavaScript. Please help

Hi,

I can’t wrap my head around this one. I’m trying to focus on a specific textarea using Javascript but I can’t get it to work and I’m not entirely sure I’m writing the instruction right.

Here’s my code:

MotivationTextID = WebUI.getAttribute(findTestObject(‘Object Repository/Page_Rdgivarstdet/textarea_freeTextField1MotivationsText’), ‘id’)

WebUI.executeJavaScript(“document.getElementById(‘MotivationTextID’).focus();”, null)

and I get this error Unable to execute javascript at… Caused by: org.openqa.selenium.JavascriptException: javascript error: Cannot read property ‘focus’ of null

Thank you in advance.

This means everything leading up the focus returns null. In other words, this:

document.getElementById(‘MotivationTextID’)

is failing and returning no element (or null).

Check you have the correct id.
Check the element itself is ready for interaction (is it present? visible?)

You can test the id in the browser devtools console. Paste this into the console:

document.querySelector('#MotivationTextID')

Hello thanks for replying. I removed the quotes like you said and now I get a different error:

Caused by: org.openqa.selenium.JavascriptException: javascript error: MotivationTextID is not defined

it seems I can’t even execute “getElementById” at all. I tried this too to see if at least I’m able to interact with the object but it didn’t work:

TestObject MotivationText1 = findTestObject(‘Object Repository/Page_Rdgivarstdet/textarea_freeTextField1MotivationsText’)

String MotivationTextID = WebUI.getAttribute(MotivationText1, ‘id’)

WebElement Motivationpb1 = WebUI.executeJavaScript(“return document.getElementById(MotivationTextID);”, null)

WebUI.executeJavaScript(“arguments[0].value=‘Hello world’;”, Arrays.asList(Motivationpb1))

Sorry. I’m an idiot before my first cup of coffee in the morning. Try this:

String MotivationTextID = WebUI.getAttribute(MotivationText1, 'id')
String js = 'document.querySelector("#' + MotivationTextID + '").focus()'
WebUI.executeJavaScript(js, null)

I’ll delete the other crap I wrote.

1 Like

BRILLIANT! It works! I had no idea you could do it that way. I assume you can do it in a similar fashion like this???

String js = ‘document.getElementById("#’ + MotivationTextID + ‘")’
WebElement Motivation1 = WebUI.executeJavaScript(js, null)

Remove the # if you’re going to use the (rather old) getElementById

But I’m not sure why you want to return the element like that? To my mind, if you drop into JavaScript, you should do as much as you can right there before dropping back into regular Groovy. Make sense?

Yeah of course. I was just curious. I’ve been struggling with getElementById now for a while so I just wanted to make sure I understand the principle.

Thank you so much!

Use querySelector instead. It takes a CSS selector. Look back and see where I used it.

You would also benefit from using Groovy’s multiline GStrings - they use triple-double-quotes:

String js = """
  // a line of js
  // second line of js
  // and so on...
"""
WebUI.executeJavaScript(js, null)

I have a lot to learn. It looks like you can do a bunch of stuff with querySelector. Looks much more comfortable than using keywords. I will look into GStrings :slight_smile:

1 Like