1

I am trying to find the following element and enter text into it. I have tried a number of different ways to access the element but always get the same error. My current line of code

searchTerm = driver.FindElement(By.Id("keyword"));

generates the same error

Unable to locate element: {"method":"id","selector":"keyword"}

The element, shown below, clearly has the Id 'keyword'.

<input maxlength="100" size="20" value="" name="keyword" id="keyword" title="keyword" class="FORMshrt2">

I used firebug to capture the complete XPath for this element.

/html/body/div/span/table[3]/tbody/tr/td/table[1]/tbody/tr[2]/td/div[1]/span/form/div[3]/table[3]/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[11]/td[2]/span/input

How do I access this element?

JWJ
  • 21
  • 4
  • I need to add that adding a wait period is not the answer. I have stepped through the program and waited until the page is completely loaded and I have verified manually that the element exists. I still cannot get the program to find this element. I also know that there are iframes on the page, but changing which iframe I am referencing doesn't seem to help either. – JWJ Oct 07 '15 at 21:51
  • Is this element visible on the page? Selenium will only interact with visible elements by design. – JeffC Oct 08 '15 at 00:50
  • Check if your element is within an iframe. If it is, try this solution: https://stackoverflow.com/a/9652932/2285470 – iamkenos May 23 '17 at 03:05

3 Answers3

0

Try closing tag input with /> like explained here.

<input maxlength="100" size="20" value="" name="keyword" id="keyword" title="keyword" class="FORMshrt2" />

I don't know if Selenium expects this closing tag, but everything else look ok.

Community
  • 1
  • 1
robsonrosa
  • 2,548
  • 5
  • 23
  • 31
0

The element might not have appeared at the moment you've started looking for it. Wait for the element to become present in the DOM:

IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.Id("keyword"))));
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
-1

Use wait statement then try the below code

you can just try the following code, which may help for your case,

Thread.Sleep(5000);

driver.FindElement(By.XPath("//input[@class='FORMshrt2']")).Click();
driver.FindElement(By.XPath("//input[@class='FORMshrt2']")).SendKeys("your text");

By using your class name i'm identifying the element, first clicking on it and then passing the string.

Deepak_Mahalingam
  • 454
  • 1
  • 9
  • 21
  • Tried this and got the same result; Unable to locate element: {"method":"xpath","selector":"//input[@class='FORMshrt2']"} – JWJ Oct 07 '15 at 21:58