I have a HTML element like this
This error message...
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'
...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'
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:
Using 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)
Using 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