1

I'm trying to get Python / Selenium to properly click on a dropdown menu and select "IT" but the best I could do was find the element and get an error stating I couldn't input text on what I have found.

Basically, the user would click on that menu and select IT or type IT and press enter.

HTML code:

<div class="form-group">

        <label class="col-md-3 control-label" for="id_tenant_group">Tenant group</label>
        <div class="col-md-9">
            <select name="tenant_group" nullable="true" class="netbox-select2-api form-control" data-url="/api/tenancy/tenant-groups/" data-filter-for-tenant="group_id" placeholder="None" id="id_tenant_group">
  <option value="" selected>---------</option>

  <option value="2">IT</option>

  <option value="1">OT</option>

</select>



        </div>

When I inspect the element, I can see there is a span element that triggers an event showing another span where finally my option can be seen.

I cannot select via visible text, because other menus also contain the same "---------" visible text.

I've captured a couple screenshots to illustrate the issue, hope that helps. html code browser inspect

To be honest I am really lost, any suggestions will be much appreciated.

EDIT: I've attempted the following:

tenant_g_element = Select(browser.find_element(By.XPATH, '//span[@id="select2-id_tenant_group-container"]'))
tenant_g_element.selectByVisibleText("IT")

But I've got the following error:

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on span
Guy
  • 34,831
  • 9
  • 31
  • 66

3 Answers3

0

Your first line is wrong. It should be something like this:

tenant_g_element = Select(browser.find_element(By.ID, 'id_tenant_group'))

because your has the attribute id="id_tenant_group". There is no need to use By.XPATH...if you have some reason to need that, then you'll need to look up how to specify an XPATH (note that //span is going to find a <span>, not a <select> for example).

Shaheed Haque
  • 365
  • 1
  • 11
  • I've tested your method with the following code: _tenant_g_element = Select(browser.find_element(By.ID, 'id_tenant_group')) tenant_g_element.select_by_visible_text("IT")_ and got the error **selenium.common.exceptions.ElementNotInteractableException: Message: Element – Renan Hingel Feb 11 '20 at 12:41
  • Well, that means that the item was found, but was not able to be scrolled into view, which a different problem. I also notice that all the other answers you were given had the same new problem.The standard StackOverflow approach would be to post your updated code in a new question specific to this issue. – Shaheed Haque Feb 11 '20 at 15:54
  • Hey Shaheed, I've found a way to post the data I needed via JSON, so getting this script to work won't be necessary anymore. I am thankful for your support. – Renan Hingel Feb 13 '20 at 12:24
0

Your Xpath seems wrong. You are trying to locate a span tag but you should locate select element. Additionally you should apply webdriver wait for of select element

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


    select = Select(WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.ID, "id_tenant_group"))))
    WebDriverWait(driver, 50).until(
        EC.presence_of_element_located((By.XPATH, "//select[@id='id_tenant_group']/option[2]")))
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='id_tenant_group']//options[contains(.,'IT')]")))
    select.select_by_visible_text('IT')
Muzzamil
  • 2,419
  • 2
  • 7
  • 21
  • Thanks for the response. I've tested the code you supplied and got the following error: **Traceback (most recent call last): File "subnet.py", line 35, in select.select_by_visible_text('IT') AttributeError: 'FirefoxWebElement' object has no attribute 'select_by_visible_text'** – Renan Hingel Feb 11 '20 at 12:46
  • @RenanHingel Answer is updated. please try now and let me know. – Muzzamil Feb 11 '20 at 13:39
  • Muzzamil, I'm thankful for your interest in this subject. I have tested your suggestion and got the following error: **selenium.common.exceptions.ElementNotInteractableException: Message: Element – Renan Hingel Feb 11 '20 at 14:01
  • @RenanHingel I have updated answer as per error message. Try Now. – Muzzamil Feb 11 '20 at 14:24
  • Hey Muzzamil, I've found a way to post the data I needed via JSON, so getting this script to work won't be necessary anymore. I am thankful for your support. – Renan Hingel Feb 13 '20 at 12:23
0

This error message...

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on elements, not on span

...implies that the element which you are passing to Select class is not a <select> node but a <span> element which isn't supported.


Seems your locator was identifying a <span> element instead of the <select> element. Hence you see the error. Additionally, selectByVisibleText() is not a valid Python method, instead you need to use select_by_visible_text().

Solution

To click on the option with text as IT, you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and select_by_value():

    tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.netbox-select2-api.form-control#id_tenant_group[name='tenant_group']"))))
    tenant_g_element.select_by_value("2")
    
  • Using XPATH and select_by_visible_text():

    tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='netbox-select2-api form-control' and @id='id_tenant_group'][@name='tenant_group']"))))
    tenant_g_element.select_by_visible_text("IT")
    
  • 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
    from selenium.webdriver.support.ui import Select
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Thanks for the response, I've tested your XPATH suggestion and I've got the following error : raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Renan Hingel Feb 11 '20 at 12:27
  • The CSS_SELECTOR gave a different error: Traceback (most recent call last): File "subnet.py", line 39, in tenant_g_element.select_by_value("2") selenium.common.exceptions.ElementNotInteractableException: Message: Element – Renan Hingel Feb 11 '20 at 12:35