Skip to main content
 首页 » 编程设计

python之为什么我不能将 "send_keys"添加到带有 selenium 的文本框属性错误 : 'NoneType'

2025年05月04日10jyk

我在填写本网站的文本框时遇到了严重困难。该元素是 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 

错误

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' 

最后输出打印语句:

Initiate Search 
Clicked Product ID 

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> 

请您参考如下方法:

你的问题在这里:

    try: inputElement = driver.find_element_by_name("CategoryName").click() 

我不确定在 python 中 inputElement 是什么,但我猜它仍然是 null。我认为 click() 不会返回任何内容。

如果你把它改成这样,它应该可以工作:

try: inputElement = driver.find_element_by_name("CategoryName") 
    inputElement.click()