0

I've been trying to select a drop-down menu but I noticed that its XPath and ID changes every time, so it is probably dynamic. How do I capture the correct path for my element on this type of conditions? We are trying to get the CSV on the drop-down menu

<div class="form-group">
        <label>Report Type</label>
    <div>
    <select data-dom-uuid="" tabindex="-1" data-name="Report Type" data-input-id="attached_report_type" data-type="select" class="editor-input select2-hidden-accessible" aria-label="Report Type" aria-hidden="true">


            <option value="csv">CSV</option>
            <option selected="selected" value="db">db</option>
            <option value="pdf">PDF</option>


    </select><span class="select2 select2-container select2-container--db select2-container--below select2-container--open" dir="ltr" style="width: 100px;"><span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="true" tabindex="11" aria-labelledby="select2-bka6-container" aria-owns="select2-bka6-results" aria-activedescendant="select2-bka6-result-vwdq-db"><span class="select2-selection__rendered" id="select2-bka6-container" title="db">db</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>

</div>
</div>

We have tried these options but none of these work,

#driver.find_element_by_xpath("/html/body/span/span").click()
    #driver.find_element_by_xpath("//*[@id=select2-7h5y-result-ycb2-csv]").click()
    #driver.find_element_by_id("id=select2-mrbe-container]").click()
    #driver.find_element_by_xpath("/html/body/section[3]/section[3]/section/form/div[1]/div/div/div[1]/section/div[3]/div[1]/div[1]/div[2]/div[6]/div[1]/div/select").click()
    select = Select(driver.find_element_by_xpath("//select[@data-input-id='attached_report_type']"))
    select.select_by_value("CSV").click()
  • 1
    What about `data-name="Report Type"`? is it fixed or dynamic? – Saurabh Gaur Jun 25 '19 at 12:47
  • I don't think that element `data-input-id` is dynamic as it is clear static text, how did you know that every thing is dynamic here? – Saurabh Gaur Jun 25 '19 at 12:49
  • I think the the **data-input-id** is since it is just the same, but this one **id="select2-8qde-container"**, this one changes everytime – thePhonenix18 Jun 25 '19 at 13:00
  • Can you share more 4-5 lines above HTML content of target element? – Saurabh Gaur Jun 25 '19 at 13:12
  • What about `data-name="Report Type"` that Saurabh asked about? That seems like a good candidate. – JeffC Jun 25 '19 at 13:16
  • Possible duplicate of [Find element by attribute](https://stackoverflow.com/questions/26304224/find-element-by-attribute) – JeffC Jun 25 '19 at 13:17
  • @SaurabhGaur I have edited the HTML code above to after Inspecting element. Does that answer your question? that select attribute might be the close one? Or that is also wrong? – thePhonenix18 Jun 25 '19 at 13:57
  • could you be more specific about the statement "none of these work"? What error or exception do you get? I would expect the "//select[@data-input-id='attached_report_type']" to work, but perhaps it is not unique on the page (it SHOULD be, but I see bad code using the same "id" multiple times). – Breaks Software Jun 25 '19 at 17:07
  • It looks like, the select box generated using bootstrap, so that, you cann't directly interact with select tag. you need alternate approach to select the values. – Murthi Jun 26 '19 at 05:27

2 Answers2

0

Please use ByTagName

driver.findElement(By.tagName("select"));
Arun Nair
  • 425
  • 3
  • 11
0

As you have posted HTML, I think data-name="Report Type" is the static and enough to find the target select element because in the HTML label is also the same. You should use following locators with explicit wait :

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

wait = WebDriverWait(driver, 10)
  1. By cssSelector :

    element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "select[data-name='Report Type']")))
    
  2. By xpath :

    element = wait.until(EC.presence_of_element_located((By.XPATH, "//label[text()='Report Type']/following::select[@data-name='Report Type']")))
    

Now you can use any of the above strategy locator to find target element and work with Select :

select = Select(element)
select.select_by_value("CSV").click()

Hope it helps

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73