python - 属性错误: 'WebElement' object has no attribute 'copy' error when moved the function Select to a common file using Selenium Python through Django

标签 python django selenium selenium-webdriver webdriver

我有一个像这样的 HTML 元素

<select id="my_id">
<option value="">ALL</option>
<option value="1.0">ALL</option>
<option value="2.0">A</option>
<option value="3.0">B</option>
<option value="4.0">C</option>
</select>

当我在测试文件中使用定义函数时,我想选择并选择一个值 一切正常

my_test_file.py
def _find_and_select(self, elm_id, value):
    select_item = Select(self.browser.find_element_by_id(elm_id))
    select_item.select_by_value(value)
self._find_and_select("my_id", "1.0")

但是当我转向通用测试文件时

common_file.py
class Common:
    @staticmethod
    def _find_and_select(browser, elm_id, value):
        select_item = Select(browser.find_element_by_id(elm_id))
        select_item.select_by_value(value)

my_test_file.py
Common._find_and_select(self.browser, "my_id", "1.0")

会出现错误:

Traceback (most recent call last):
  File "D:\iBNet-Prj\ibnet\apps\autotest\contract\tests.py", line 251, in test_search
    CommonTest._find_and_select(self.browser, "contractLoanStatus", loanStatus[0])
  File "D:\iBNet-Prj\ibnet\apps\common_test.py", line 467, in _find_and_select
    select_item = Select(browser.find_element_by_id(elm_id))
  File "D:\iBNet-Prj\venv\lib\site-packages\django\forms\widgets.py", line 558, in __init__
    super().__init__(attrs)
  File "D:\iBNet-Prj\venv\lib\site-packages\django\forms\widgets.py", line 201, in __init__
    self.attrs = {} if attrs is None else attrs.copy()
AttributeError: 'WebElement' object has no attribute 'copy'

最佳答案

此错误消息...

  File "D:\iBNet-Prj\ibnet\apps\common_test.py", line 467, in _find_and_select
    select_item = Select(browser.find_element_by_id(elm_id))
  File "D:\iBNet-Prj\venv\lib\site-packages\django\forms\widgets.py", line 558, in __init__
    super().__init__(attrs)
  File "D:\iBNet-Prj\venv\lib\site-packages\django\forms\widgets.py", line 201, in __init__
    self.attrs = {} if attrs is None else attrs.copy()
AttributeError: 'WebElement' object has no attribute 'copy'

...暗示代码行 select_item = Select(browser.find_element_by_id(elm_id)) 失败,并且您正在使用 调用框架 super().__init__(attrs) 并产生错误:

AttributeError: 'WebElement' object has no attribute 'copy'

解决方案

要理想地选择所需的元素,您必须为 element_to_be_clickable() 引发WebDriverWait,并且您可以使用以下任一 Locator Strategies :

  • 使用CSS_SELECTOR:

    select_item = Select(WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#my_id"))))
    select_item.select_by_value(value)
    
  • 使用XPATH:

    select_item = Select(WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='my_id']"))))
    select_item.select_by_value(value)
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

关于python - 属性错误: 'WebElement' object has no attribute 'copy' error when moved the function Select to a common file using Selenium Python through Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59783130/

相关文章:

selenium - xvfb 和 thucydides/Selenium 运行 Firefox 的默认路径

python - 为什么不同的格式方法在 Python 中表现不同?

python - Pandas 中数据帧的自定义计算函数

Python:如何使用结构来打包和解包对对象的引用?

python - Postgres 突然出现错误 '/usr/lib/libpq.5.dylib'(没有这样的文件)

python - 使用 selenium xpath 定位元素

python - 复制特定层的权重 - keras

python - Django Shell API KeyError

django - 禁用 Django 中特定应用程序的本地化

selenium - 使用 Selenium-Webdriver 2.0 时如何加快 Internet Explorer 中的 sendkeys() 速度?