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

关于python - 为什么我不能将“send_keys”添加到含 Selenium 的文本框中? AttributeError:“NoneType”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22566118/

10-15 06:13