0
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?

  • Try this, might this work for you :- WebElement username = driver.findElement(By.xpath(".//input[@name='username']")); – Upkar Singh Apr 18 at 10:32
  • getting same issue. i think it's not related to code cause i've tried each method of it (xpath, cssSelector etc.) as i said, code was working before but right now concerned element can't be captured – santino87 Apr 18 at 11:04
  • What troubleshooting steps have you taken already? – anonygoose Apr 18 at 11:24
  • Network speed might be greater on LAN, maybe that affected the load times. – Mate Mrše Jun 27 at 8:36
1

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)..

0

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).

0

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']")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.