【问题标题】:Element not found for Name未找到名称的元素
【发布时间】:2021-05-17 01:36:50
【问题描述】:

我遇到了一个找不到元素的问题 我的代码:

    Dim bot As WebDriver
    
    Set bot = New WebDriver
    bot.Start "chrome"
    bot.Get "http://..."
    bot.FindElementByName("userName").SendKeys "text"
    bot.FindElementByName("password").SendKeys "text"
    bot.FindElementByClass("ButtonLast").Click
    
    bot.FindElementByName("searchField").SendKeys "text"
    
    Stop

HTML:

<input
    class="SearchRoundCorner"
    onblur="searchFieldInnerTxt(this);"
    onfocus="searchFieldInnerTxt(this);"
    style="width: 150px; padding-left: 20px; color: rgb(128, 128, 128);"
    type="text"
    name="searchField"
    value="search"
    title=""
    onkeypress="if ((window.event &amp;&amp;  window.event.keyCode == 13)|| event.which==13){ document.ListView.pageNo.value=1;document.ListView.saveSelection.value = 0;submitForm(document.ListView);return false; }"
/>

它运行良好,直到我到达那一行-bot.FindElementByName("searchField").SendKeys "text"

错误:

Error message:Run-Time Error 7 NoSuchElementError Element not found for name=searchfield

【问题讨论】:

  • 请用网址、整页源代码和图片编辑您的问题。
  • 你的 dom 中有搜索按钮,还是需要等待一段时间?..

标签: excel vba selenium xpath css-selectors


【解决方案1】:

所需元素是动态元素,因此要将字符序列发送到元素,您可以使用以下任一Locator Strategies

  • 使用FindElementByName

    bot.FindElementByName("searchField").SendKeys "text"
    
  • 使用FindElementByCss

    bot.FindElementByCss("input.SearchRoundCorner[name='searchField'][value='search']").SendKeys "text"
    
  • 使用FindElementByXPath

    bot.FindElementByXPath("//input[@class='SearchRoundCorner' and @name='searchField'][@value='search']").SendKeys "text"
    

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2011-09-18
    • 2011-03-12
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多