为什么我不能用selenium"send_keys"到文本框?AttributeError:'NoneType'

Exp*_*ode 1 python selenium nonetype

我在填写本网站的文本框时遇到了严重困难.元素是type ="text".它继续作为AttributeError返回:'NoneType'.

我使用了一个try子句来查看它是否实际被点击,并且它没有错误.此外,选择了文本框,当我将其保留为前窗时,我可以在发生错误后键入文本框而不单击任何内容.

我尝试将点击之前的暂停延长1分钟无效.

我尝试通过xpath选择,结果相同.

我尝试单击该对象,暂停10秒,然后再次单击该对象.没有.

我的代码:

def Search(driver,productID):
    print "Initiate Search"

    #Fill in current product ID
    #/html/body/table[3]/tbody/tr/td/table/tbody/tr/td[2]/div[2]/form/table/tbody/tr/td/input

    try: inputElement = driver.find_element_by_name("CategoryName").click()
    except: print "could not click"
    print "Clicked Product ID"

    time.sleep(5)
    inputElement.send_keys(str(productID))  ##Line 105 - Errors out here
Run Code Online (Sandbox Code Playgroud)

错误

Traceback (most recent call last):
  File "/Users/ME/Documents/PYTHON/Creating Static Attributes/StaticWAttributes_1.py", line 246, in <module>
    Search(driver,productID)
  File "/Users/ME/Documents/PYTHON/Creating Static Attributes/StaticWAttributes_1.py", line 105, in Search
    inputElement.send_keys(str(productID))
  AttributeError: 'NoneType' object has no attribute 'send_keys'
Run Code Online (Sandbox Code Playgroud)

最后输出打印声明:

Initiate Search
Clicked Product ID
Run Code Online (Sandbox Code Playgroud)

HTML:

  <table cellSpacing="0" cellPadding="0" border="0">
        <tr>
          <td class="actionrow">Search Products by  
          <select name="categorytype">
                        <option selected value="name">Product Name or Description</option>
                        <option  value="catid">Product ID</option>
                  </select> <input type="text" name="CategoryName" value="" size="20"><? <<-- I AM TRYING TO CLICK THIS ?>
                  &nbsp;in&nbsp;
                  <select name="ddlproductType" ID="Select2">
                    <option selected value="100">All</option>
                        <option  value="1">Versioned</option>

                            <option  value="7">Variable</option>

                        <option  value="5">Static with Attributes</option>
            <option  value="11">PowerPoint</option>
                  </select>&nbsp;
                <input type="submit" value="Go" name="action" onClick="javascript:resetAll();"> 
          </td>
        </tr>
  </table>
Run Code Online (Sandbox Code Playgroud)

Ric*_*ard 9

你的问题在这里:

    try: inputElement = driver.find_element_by_name("CategoryName").click()
Run Code Online (Sandbox Code Playgroud)

我不确定inputElementpython的情况是什么,但我猜它仍然是null.我不认为click()什么回报.

如果你把它改成这个,它应该工作:

try: inputElement = driver.find_element_by_name("CategoryName")
    inputElement.click()
Run Code Online (Sandbox Code Playgroud)