简体   繁体   English

Selenium 自动化测试错误:TypeError: 'WebElement' object is not callable

[英]Selenium automated testing error: TypeError: 'WebElement' object is not callable

I am using Selenuium to test a Django website.我正在使用 Selenuium 测试 Django 网站。

When I run my test, I get the following error:当我运行测试时,我收到以下错误:

TypeError: 'WebElement' object is not callable类型错误:'WebElement' object 不可调用

Here are the relevant snippets of my code:以下是我的代码的相关片段:

base.py基础.py

import os
import sys

from pathlib import Path
from selenium import webdriver
from selenium.webdriver.remote.remote_connection import LOGGER as SELENIUM_LOGGER, logging as selenium_logging
from webdriver_manager.chrome import ChromeDriverManager

sys.path.insert(0, str(Path(os.path.dirname(__file__)).parent.absolute()) )

from django.urls import reverse
from django.test import TestCase

from django.contrib.auth import get_user_model


User = get_user_model()


TESTING_URL_ROOT = os.environ.get('TESTING_URL_ROOT', 'http://127.0.0.1:8000')


# https://stackoverflow.com/questions/23407142/how-do-i-reduce-the-verbosity-of-chromedriver-logs-when-running-it-under-seleniu
SELENIUM_LOGGER.setLevel(selenium_logging.WARNING)


# https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class
class BaseTestCases:

    class BaseTest(TestCase):

        def setUp(self): 
            chrome_options = webdriver.ChromeOptions()
            chrome_options.headless = True
            chrome_options.add_argument("--disable-notifications")

            chrome_options.add_argument("disable-infobars"); # disabling infobars
            chrome_options.add_argument("--disable-extensions"); # disabling extensions
            chrome_options.add_argument("--disable-gpu"); # applicable to windows os only
            chrome_options.add_argument("--disable-dev-shm-usage"); # overcome limited resource problems
                        
            chrome_options.add_argument(f"user-data-dir={str(Path.home())}") # https://stackoverflow.com/questions/31062789/how-to-load-default-pro
            chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")

            self.browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

            # ...


        def tearDown(self):  
            self.browser.quit()


        # For Django client object, which can work with relative URLs
        def get_relative_url_from_route_name(self, route_name):
            route_name = route_name.strip()
            return f"{reverse(route_name)}"
 

        # for selenium, which requires FULL URL
        def get_full_url_from_route_name(self, route_name):
            route_name = route_name.strip()
            if not route_name:
                return TESTING_URL_ROOT
            else:
                return f"{TESTING_URL_ROOT}{reverse(route_name)}"


        # Default is set to True, because Selenium provides better UI testing
        # e.g. Javascript functionality in web page etc. 
        def resolve_url(self, route_name, full_url_path=True):
            if full_url_path:
                return self.get_full_url_from_route_name(route_name)
            else:
                return self.get_relative_url_from_route_name(route_name)

visitor.py访客.py

from base import BaseTestCases

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By


class VisitorTest(BaseTestCases.BaseTest):  
    # ...
    def test_home_page_shows_join_us_button(self):
        url = self.resolve_url("home:index")
        self.browser.get(url)
        error_flag = self.assertRaises(NoSuchElementException, self.browser.find_element_by_id('join-us'))
        self.assertFalse(error_flag) 

    # ...

Stack trace堆栈跟踪

ERROR: test_home_page_shows_join_us_button (visitor.VisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/visitor.py", line 63, in test_home_page_shows_join_us_button
    error_flag = self.assertRaises(NoSuchElementException, self.browser.find_element_by_id('join-us'))
  File "/usr/lib/python3.8/unittest/case.py", line 816, in assertRaises
    return context.handle('assertRaises', args, kwargs)
  File "/usr/lib/python3.8/unittest/case.py", line 202, in handle
    callable_obj(*args, **kwargs)
TypeError: 'WebElement' object is not callable

What is causing this error - and how do I fix it?是什么导致了这个错误 - 我该如何解决?

Here is the relevant version information:以下是相关版本信息:

Selenium: 3.141.0
Django: 3.2
Python: 3.8.5
OS: Linux (Ubuntu 12.0.4 LTS)
Chrome:
  Current google-chrome version is 90.0.4430
  Get LATEST driver version for 90.0.4430

I guess the issue here is that you are trying to find_element_by_id('join-us') element immediately after self.browser.get(url) while the web page is still not loaded so the element you are looking for is still not presented so find_element_by_id('join-us') returns null我想这里的问题是你试图在self.browser.get(url)之后立即find_element_by_id('join-us')元素,而 web 页面仍然没有加载所以你正在寻找的元素仍然没有出现所以find_element_by_id('join-us')返回null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 类型错误:'WebElement' object 不可调用 - Selenium TypeError: 'WebElement' object is not callable 类型错误:'WebElement' object 不可调用 - TypeError: 'WebElement' object is not callable 硒错误“类型错误:‘str’对象不可调用” - Selenium error "TypeError: 'str' object is not callable" TypeError: 'WebElement' object is not subscriptable error submit form using Selenium and Python - TypeError: 'WebElement' object is not subscriptable error submitting form using Selenium and Python TypeError: 'WebElement' 对象不是可迭代的错误 - TypeError: 'WebElement' object is not iterable error Selenium / Python TypeError:“ WebElement”对象不可迭代 - Selenium / Python TypeError: 'WebElement' object is not iterable TypeError:使用Selenium ChromeDriver Chrome和Python时,'module'对象无法调用错误 - TypeError: 'module' object is not callable error using Selenium ChromeDriver Chrome with Python 类型错误:“模块”对象不可调用错误运行 pip install selenium - TypeError: 'module' object is not callable error running pip install selenium 如何解决 TypeError: 'module' object is not callable 错误使用 Selenium 和 ChromeDriver - How to address TypeError: 'module' object is not callable error using Selenium and ChromeDriver TypeError: 'str' object is not callable error using text() from Selenium Python - TypeError: 'str' object is not callable error using text() from Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM