【问题标题】:Unable to find Element via XPath Selector for type text with no name or Id无法通过 XPath 选择器为没有名称或 ID 的类型文本找到元素
【发布时间】:2021-12-21 00:25:39
【问题描述】:

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

【问题讨论】:

    标签: c# selenium selenium-webdriver selenium-chromedriver selenium-ide


    【解决方案1】:

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    你应该检查的xpath:

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

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654329@匹配节点。

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

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

    我不确定你为什么需要一个动作类。

    【讨论】:

    • 感谢您的帮助,您需要在此@placeholder='search' @type='text' 之间再添加一个
    【解决方案2】:

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

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

    $x("//input")
    

    然后如果没有找到,像这样切换到不同的 iframe:

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

    【讨论】:

    • 是的,这种情况也是正确的,所以投了赞成票
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    相关资源
    最近更新 更多