'NoneType' object has no attribute 'find_element'

问题描述:


提问:命名获取到了loc的值,为啥说空类型对象没有find_element属性?应该怎么写呢


class Base:
    def __init__(self,driver):
        self.driver = driver

    #定位元素方法 封装
    def base_find(self,loc,timeout=30,poll=0.5):
        # 使用显示等待 查找元素
        print(loc)
        return WebDriverWait(self.driver,timeout=timeout,
                             poll_frequency=poll).until(lambda x:x.find_element(*loc))

运行结果:

C:\Users\lihao\AppData\Local\Programs\Python\Python36\python.exe "D:\Pycharm\PyCharm 2018.3.3\helpers\pycharm\_jb_unittest_runner.py" --target test01_login.TestLogin
Launching unittests with arguments python -m unittest test01_login.TestLogin in E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\testcase
('partial link text', '登录')


Ran 0 tests in 13.137s

FAILED (errors=1)

Failure
Traceback (most recent call last):
  File "C:\Users\lihao\AppData\Local\Programs\Python\Python36\lib\unittest\suite.py", line 163, in _handleClassSetUp
    setUpClass()
  File "E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\testcase\test01_login.py", line 24, in setUpClass
    cls.login.page_click_login_link()
  File "E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\Page\page_login.py", line 8, in page_click_login_link
    self.base_click(Page.login_link)
  File "E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\Base\base.py", line 18, in base_click
    self.base_find(loc).click()
  File "E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\Base\base.py", line 15, in base_find
    poll_frequency=poll).until(lambda x:x.find_element(*loc))
  File "C:\Users\lihao\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "E:\PythonWork\自动化测试\WebUI\ShopXO商城项目实战\Base\base.py", line 15, in <lambda>
    poll_frequency=poll).until(lambda x:x.find_element(*loc))
AttributeError: 'NoneType' object has no attribute 'find_element'


Process finished with exit code 1
 

定位方式有问题,这里虽然能够输出loc的值,但是通过这个loc定位方式无法找到登陆按钮。

partial link text表示查找a标签中的部分文字,这里的登陆按钮虽然可以点击,但并不是一个a标签链接。

查看一下要找的登陆按钮的id,通过id来定位。

您好,我是问答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632