【问题标题】:AttributeError: 'NoneType' object has no attribute 'get_attribute'AttributeError:“NoneType”对象没有属性“get_attribute”
【发布时间】:2021-10-10 16:43:07
【问题描述】:

我想提取输入到网站文本框中的输入的长度,以查看它允许的最大字符数。 HTML 说最大长度是 40,所以我尝试输入 41 个字符,看看它是否只会输入前 40 个字符。当我运行我拥有的代码时,我得到 AttributeError: 'NoneType' object has no attribute 'get_attribute'

这是我目前的代码:

def test_FN_SPmax(self):
    time.sleep(3)
    first_name = self.driver.find_element_by_xpath(
        '/html/body/div[4]/div[1]/section/div[2]/div[1]/div[4]/div/div/div/div/div/div[2]/div/div[1]/section/div/section/div/div/div/div/div/div[1]/div/div/div/fieldset/div/div[2]/input').send_keys(
        ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(41)))
    time.sleep(2)
    typedValue = first_name.get_attribute('value')
    size = typedValue.length()
    print(size)

【问题讨论】:

    标签: python selenium selenium-webdriver pycharm python-unittest


    【解决方案1】:
    first_name 
    

    不再是网络元素,因为您确实使用了.send_keys

    所以这样做吧:

    first_name = self.driver.find_element_by_xpath('/html/body/div[4]/div[1]/section/div[2]/div[1]/div[4]/div/div/div/div/div/div[2]/div/div[1]/section/div/section/div/div/div/div/div/div[1]/div/div/div/fieldset/div/div[2]/input')
    first_name.send_keys('something here')
    time.sleep(2)
    typedValue = first_name.get_attribute('value')
    

    应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-01
      • 2021-12-26
      • 2019-07-23
      • 2018-05-13
      • 2020-09-07
      • 2017-05-03
      • 2023-03-16
      • 2018-07-14
      相关资源
      最近更新 更多