Mandy
Mandy

Reputation: 475

Webdriver focus event : js.executeScript ("document.getElementByName(' ').focus()");

I'm working on an application that does not contain id attribute for few textboxes .Other txtboxes have id.If I don't use the focus event I'm not able to use 'sendKeys' for setting the values for textboxes. Hence I used

js.executeScript ("document.getElementById('x').focus()"); 

But since some textboxes don't have an id attribute i can't use the above hence tried using

js.executeScript ("document.getElementByName('xyz').focus()");

Ondoing this following exception msg is generated:

org.openqa.selenium.WebDriverException: document.getElementByName is not a function (WARNING: The server did not provide any stacktrace information); duration or timeout: 26 milliseconds

Upvotes: 1

Views: 7486

Answers (2)

JacekM
JacekM

Reputation: 4099

You could try clicking on element to set focus.

driver.findElement(By.name("someName")).click();

Upvotes: 1

Justin Ko
Justin Ko

Reputation: 46836

I do not believe there is a getElementByName. You need to do getElementsByName (note the 's' for Elements).

getElementsByName returns a collection, so you will need to specify the index:

js.executeScript ("document.getElementsByName('xyz')[0].focus()");

Upvotes: 1

Related Questions