Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select any element from the web element with "display: none" attribute using Selenium in Python

I need to select any element from the web element with display: none attribute that looks like this:

<div class="some_class">
  <select id="some_id" class="some_select_class" style="display: none;">
    <option value="1" data-isleaf="false" data-catid="3" data-special_message="" data-adtypeid="0">1</option>
    <option value="2" data-isleaf="true" data-catid="4" data-special_message="" data-adtypeid="1">2</option>    
  </select>
</div>

I can do it manually from a web browser, but I need to do it via Selenium in Python. Unfortunately, when I have the following code:

try:
  element = selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_id')))
  selenium.webdriver.support.ui.Select(element).select_by_value('1')
except Exception as ex:
  print(ex)

WebDriverWait throws an exception with the following info:

Message: ''

The type of the exception is selenium.common.exceptions.TimeoutException

How can I achieve the interaction of this element? How can I select any element in this case?

Thanks in advance.

like image 541
FrozenHeart Avatar asked Feb 12 '23 13:02

FrozenHeart


1 Answers

Use execute_script() to set the display property of that element and then use the Selenium Select for selecting a required value.

Below code should work for you:

    try:
      selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_other_id_on_page')))
      selenium.execute_script("document.getElementById('some_id').style.display='inline-block';")
      element = selenium.webdriver.support.ui.WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, 'some_id')))
      selenium.webdriver.support.ui.Select(element).select_by_value('1')
    except Exception as ex:
    print(ex)
like image 115
Surya Avatar answered Feb 16 '23 03:02

Surya