0

This is my sample html code.

<div class="content">
<M class="mclass">
   <section id="sideA">
       <div id="mainContent"> 
           <div class="requestClass"> 
              <span>Check</span>
              <input type="text" id="box">
           </div>
       </div>
   <section>
   <section id="sideB">
      ...
   <section>
</M>
</div>

I want to set some value to my text field ("box"). So I tired to set like below code

driver.findElement(By.xpath("...")).sendKeys("SetValue");

My Xpath id is correct, it's exist in the page but am getting this error

no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}

Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?

KSK
  • 636
  • 1
  • 9
  • 29

3 Answers3

1

As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:

  1. cssSelector :

    driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
    
  2. xpath :

    driver.findElement(By.xpath("//section[@id='sideA']//input[@id='box']")).sendKeys("SetValue");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

If you still want to use XPath. This worked for me-

 driver.FindElement(By.XPath(@"//*[@id='box']")).SendKeys("AB‌​");

I don't think the custom tag causes any problem as the CssSelector also works-

driver.FindElement(By.CssSelector(@"m[class='mclass'] input")).SendKeys("AB");
amitbobade
  • 500
  • 1
  • 4
  • 15
0

You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.

Using ID, your code is like this:

WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");

You can also use JavascriptExecutor

WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36