Bastian
Bastian

Reputation: 1237

Selenium search elements by css instead of id

today I update me chrome driver to

If you are using Chrome version 77, please download ChromeDriver 77.0.3865.40

I try to find element via id using this code:

public static void inputValueById(String input,String id)
    {
        WebDriver driver2 = WebDriverMgr.getDriver();
        WebElement element = driver2.findElement(By.id("//input[@id='company']"));
        element.click();
        element.clear();
        element.sendKeys(input);
    }

and I get this exception

ERROR: no such element: Unable to locate element: {"method":"css selector","selector":"#\/\/input\[\@id\=\'company\'\]"}
  (Session info: chrome=77.0.3865.90)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'C', ip: '10.9.26.18', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 77.0.3865.40 

Can someone please advise why it is searching by css selector and not by ID? method":"css selector"

Upvotes: 2

Views: 421

Answers (1)

0xM4x
0xM4x

Reputation: 470

I think when you try to find an Element by its ID you have to write your code like this:

driver.findElement(By.id("ID"))

but you wrote the xpath instead of ID. if you want to find with this xpath you have to write like this:

 driver.findElements(By.xpath("//input[@id='company']"));

Upvotes: 4

Related Questions