Search code examples
seleniumxpath

Find element by XPath isn't working in selenium


I'm trying to find a element in selenium with this XPATH /html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input.

I copied it from inspect element, and copy xpath. I saw that some persons with the same problem use "*" character but I don't know where I should to use it.

this the html code

<input type="text" data-bind="value: CorpItem.Name, valueUpdate: 'afterkeydown'"
                                                class="form-control" placeholder="Enter ..." required="required">

Here is my code on Selenium

  IWebElement corpName = driver.FindElement(By.CssSelector("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))

Solution

  • Try this XPath

    //input[@type='text'][@class='form-control']
    

    In your code you used XPath and gave selector as CSS. Please verify.

    Using your XPath, your code must be

    IWebElement corpName = driver.FindElement(By.XPath("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
    

    If it didn't work, try

    IWebElement corpName = driver.FindElement(By.XPath("//input[@type='text'][@class='form-control']"));