Mr. Ng Mr. Ng - 5 months ago 18
Java Question

Trouble Locating element on webpage

HTML of element

In selenium webdriver, I am trying to locate, then clear the textbox element in the given image with this code

driver.findElement(By.name("X")).clear();


or

driver.findElement(By.cssSelector("input[name=X][type=text]")).clear();


but neither of them work, giving the error messages:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"X"}


and

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input[name=X][type=text]"}


What am I doing incorrectly and how can fix it? Any help is appreciated, thanks in advance.

Answer

Maybe there are some javascript shenanigans going on. Try waiting for the element to be located.

WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@name='X']")));
myDynamicElement.clear();