Reputation: 475
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
Reputation: 4099
You could try clicking on element to set focus.
driver.findElement(By.name("someName")).click();
Upvotes: 1
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