I encountered an error while attempting to select a drop-down element in my code.
Error
Traceback (most recent call last):
File "C:\Webdriver\ClearCore\TestCases\OperationsPage_TestCase.py", line 59, in test_add_and_run_clean_process
process_lists_page.click_select_a_preview_to_import_configuration_from_dropdown_from_clean_task_tab(Globals.datapreview_crm_name)
File "C:\Webdriver\ClearCore\Pages\operations.py", line 113, in click_select_a_preview_to_import_configuration_from_dropdown_from_clean_task_tab
select = (Select(self.get_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[7]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[3]/div/div[4]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div/div/div/div[1]/div[1]/div[2]/span[10]/span/select')))
File "C:\Webdriver\ClearCore 501\Pages\base.py", line 31, in get_element
return element
UnboundLocalError: local variable 'element' referenced before assignment
Here is the method I implemented:
def click_select_a_preview_to_import_configuration_from_dropdown_from_clean_task_tab(self, data_preview_name):
#select = Select(By.XPATH, '//span[contains(text(), "Select a data preview to import configuration from")]/preceding::span[1]//../span//../select')
#select = (Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(), "Select a data preview to import configuration from")]/preceding::span[1]//../span//../select')))))
#select = (Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[7]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[3]/div/div[4]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div/div/div/div[1]/div[1]/div[2]/div/span[10]/span/select')))))
select = (Select(self.get_element(By.XPATH, 'html/body/div[2]/div[2]/div/div[4]/div/div[2]/div/div[3]/div/div[7]/div/div[3]/div/div[4]/div/div[2]/div/div[4]/div/div[3]/div/div[3]/div/div[4]/div/div[2]/div/div[3]/div/div[2]/div/div[3]/div/div[3]/div/div/div/div/div[1]/div[1]/div[2]/span[10]/span/select')))
print select.options
print [o.text for o in select.options]
select.select_by_visible_text(str(data_preview_name))
return self
This is how the get_element method is implemented:
def get_element(self, how, what):
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
return element
The method called from the TestCase class to interact with the drop-down:
process_lists_page.click_select_a_preview_to_import_configuration_from_dropdown_from_clean_task_tab("Selenium_LADEMO_CRM_DONOTCHANGE")
Displayed HTML structure for the drop-down:
<div id="operations_add_process_list_tab_groups_tab_standard_1">
<span/>
<span>
<span class="" title="" style="font-weight:bold;">Select a data preview to import configuration from</span>
</span>
<span>
<span/>
<span>
<span>
<span>
<span>
<span>
<select tabindex="-1">
<option selected="selected" value="None">None</option>
<option value="CRMINVALID_07102015">CRMINVALID_07102015</option>
…(Other options)…
</select>
</span>
</div>
Any insights on why this error might be occurring? Is it due to visibility issues or a race condition?
My goal is to select the option "Selenium_LADEMO_CRM_DONOTCHANGE" from the drop-down list.
Thank you, Riaz