WebElement username = driver.findElement(By.cssSelector("input[name='username']"));
The statement mentioned above was working before but right now concerned element can't be captured and error message including " Unable to find element " is popping up. What might be the reason ?
Scenario: It's login page by typing username and password.
browser: IE explorer 11 (code should be run in that browser btw.)
UPDATE: I dont know why but when I changed my connection to wifi from lan, it worked. Why would that change anything?
3 Answers 3
If it was working before, than the selector should be fine but something else has changed. Please check the site if the element changed. If not, then maybe add a wait to make sure the element is present.
Other issues could be related to the viewport, or responsive design (element is not displayed on smaller widths)..
You are probably looking for the element before it has been added to the DOM. You should probably add some sort of retry logic. Make sure that the retry reloads the DOM.
Adding wait/sleep is not a good approach, since it might make your test flaky (now or maybe later).
The approach mentioned in the above comments are correct.
In case the element locator is still same, Providing the implementation that we usually follow in our software testing companies for web application testing services
Below code snippet will check if the element is present on the DOM of a page and visible. Add this snippet before entering username
WebDriverWait wait = new WebDriverWait(driver, 180); //180 seconds(can be modified as per your requirement)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[name='username']")));
Explore related questions
See similar questions with these tags.
WebElement username = driver.findElement(By.xpath(".//input[@name='username']"));