Unable to find Element via XPath Selector for type text with no name or Id

Viewed 202

Chrome Inspect

Using Chrome

The Code in C#

IWebElement Search =
   driver.FindElement(By.XPath("//*[@placeholder='search' and
   @type='text']"));
       Actions actions_Search = new Actions(driver);
       actions_Search.MoveToElement(Search).Click().SendKeys("ABCDEFG").Perform();
2 Answers

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//input[contains(@class,'searchbox') and @placeholder='search' @type='text']

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If we have unique entry 1/1 then you can use the following code :

IWebElement elem = driver.FindElement(By.Xpath("//input[contains(@class,'searchbox') and @placeholder='search' @type='text']")).SendKeys("ABCDEFG");

I am not certain why exactly you need an action class for this.

Mostly, then you can't find element by xpath but the element is exist, is probably because it exist in different iframe

check location of element in browser from top level element by code (in chrome console):

$x("//input")

then if it is not found, switch to different iframe like this: enter image description here

Then use iframe switch in your code

Related