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?

Kate Paulk
31.5k8 gold badges56 silver badges109 bronze badges
asked Apr 18, 2019 at 10:24
4
  • Try this, might this work for you :- WebElement username = driver.findElement(By.xpath(".//input[@name='username']")); Commented Apr 18, 2019 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 Commented Apr 18, 2019 at 11:04
  • What troubleshooting steps have you taken already? Commented Apr 18, 2019 at 11:24
  • Network speed might be greater on LAN, maybe that affected the load times. Commented Jun 27, 2019 at 8:36

3 Answers 3

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

answered Apr 18, 2019 at 11:10
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).

answered Apr 18, 2019 at 12:54
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']")));
answered Apr 22, 2019 at 7:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.