1
votes

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?

3
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
Is this element visible on the page? Selenium will only interact with visible elements by design. - JeffC
Check if your element is within an iframe. If it is, try this solution: stackoverflow.com/a/9652932/2285470 - iamkenos

3 Answers

0
votes

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.

0
votes

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"))));
-1
votes

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.