无法通过 XPath 选择器为没有名称或 ID 的类型文本找到元素

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

Chrome 检查

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();

如果我们在 HTML DOM 中有 unique 条目,请检查 dev tools (Google chrome)。

你应该检查的 xpath :

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

检查步骤:

Press F12 in Chrome -> 转到 element 部分 -> 执行 CTRL + F -> 然后粘贴 xpath 并查看是否需要 element正在 突出显示 1/1 匹配节点。

如果我们有唯一条目 1/1 那么您可以使用以下代码:

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

我不确定为什么你需要为此采取行动 class。

大多数情况下,你无法通过xpath找到元素,但元素是存在的,可能是因为它存在于不同的iframe中

通过代码(在 chrome 控制台中)从顶级元素检查元素在浏览器中的位置:

$x("//input")

然后如果找不到,切换到不同的 iframe,如下所示:

然后在您的代码中使用 iframe 开关