AttributeError:将函数“选择”移动到Django时,使用Selenium Python将“ WebElement”对象没有属性“ copy”错误 [英] AttributeError: 'WebElement' object has no attribute 'copy' error when moved the function Select to a common file using Selenium Python through Django

查看:115
本文介绍了AttributeError:将函数“选择”移动到Django时,使用Selenium Python将“ WebElement”对象没有属性“ copy”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML元素

I have a HTML element like this

<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>

当我在测试文件$中使用定义函数时,我想选择并选择一个值b $ b可以正常工作

I want to select and choose an value of that, when I use define a function in test file It will working OK

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")

但是当我移至通用测试文件时

But when I move to a common test file

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)被调用并产生错误:

...implies that the line of code select_item = Select(browser.find_element_by_id(elm_id)) failed and as you are using django framework super().__init__(attrs) was invoked which produces the error:

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



解决方案



选择理想情况下,您需要为 element_to_be_clickable()诱导 WebDriverWait ,并且可以使用以下定位器策略

Solution

To select the desired element ideally you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following 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)
    


  • 注意 :您必须添加以下导入:

  • Note : You have to add the following imports :

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


  • 这篇关于AttributeError:将函数“选择”移动到Django时,使用Selenium Python将“ WebElement”对象没有属性“ copy”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆