0
for (let n = 0; n < someNumber; n++){ 
  try {
    driver.get(url[n]);
    driver.findElement(By.css("selector"));
  } catch (err){
    console.log(url[n] + "did not load");
  }
}

I am trying to loop through web pages in Javascript. I am looking for an element on the page. If I cannot find the element, I want it to log the URL and continue the loop. The problem is that my code just stops at the findElement and will stop execution. It never reaches my catch block. I just get the error below:

NoSuchElementError: no such element: Unable to locate element: {"method":"css selector"

What am I doing wrong?

2 Answers 2

1

You are using wrong selector, If you want to locate the element using cssSelector then you should write something like this :

driver.findElement(By.cssSelector("Location of your WebElement"));
Example : 
driver.findElement(By.cssSelector("input[id=email]"));

In place of : driver.findElement(By.css("selector"));

findElement will throw an error if the element you're searching for doesn't exist. And if there is only one element you are looking for then you should write findElement or if there are many elements present then use findElements

Sign up to request clarification or add additional context in comments.

1 Comment

This is in JavaScript, not Java. JavaScript Selenium locator for css correctly works the way i wrote it.
0

This error has come as you have used invalid locator. Your locator should be cssSelector not css.

Change this line driver.findElement(By.css("selector")) to driver.findElement(By.cssSelector("selector")) and your program would be working as expected.

1 Comment

This is in JavaScript, not Java. This is the correct syntax in JavaScript for locating CSS.

Your Answer

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.