0

im trying to locate an element by name or id with selenium java but i am not able to locate

System.setProperty("webdriver.chrome.driver",driverPath + "chromedriver");
System.out.println(driverPath + "chromedriver");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
String baseUrl = "xxxx";
driver.get(baseUrl);
driver.manage().window().maximize();
ByName salutation = new ByName("salutation");
wait.until(ExpectedConditions.elementToBeClickable(salutation));
WebElement root1 = driver.findElement(salutation);

And thats the message that i got. im super confused why i got this error message saying "method: css selector" because i'm obviously not using the css selector:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.name: salutation (tried for 15 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at de.xx.tests.XXTests.main(XXTests.java:29)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='salutation']"}
  (Session info: chrome=78.0.3904.108)
Guy
  • 46,488
  • 10
  • 44
  • 88
  • you say that you are "trying to locate an element by name or id", but your code is only looking for the element by name attribute. you don't show the HTML. is the "salutation" really the id, not the name? if so, you'll have to ask for it by id, not name. – Breaks Software Dec 05 '19 at 13:26

2 Answers2

1

ByName is an API defined by WebDriver and the actual implementation converts it to the CSS Selector *[name='salutation']

As you can see, it is trying to find the element by its name and is unable to find it.

Without an HTML code of the page under the test it is impossible to tell why it is not found: given that you use elementToBeClickable the element must be visible and enabled such that you can click it

Sergii Pozharov
  • 17,366
  • 4
  • 29
  • 30
1

TimeoutException is the outcome of failed ExpectedConditions. In your code block you have induced WebDriverWait for the ExpectedConditions as elementToBeClickable(WebElement element) where Selenium tried to identify the element through it's effective cssSelector:

*[name='salutation']

You can find a detailed discussion in Official locator strategies for the webdriver

Possibly the WebElement salutation was defined as either of the following locators:

  • name:

    salutation
    
  • cssSelector:

    [name='salutation']
    
  • xpath:

    //*[@name='salutation']
    

Solution

As a solution you can use a granular and a finer locator adding the tagName to the locator strategy as follows:

tagName[attributeName='attributeValue']

So your effective locators will be:

  • cssSelector:

    tagName[name='salutation']
    
  • xpath:

    //tagName[@name='salutation']
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352