How can I select an option from dropdown with selenium?

61 views Asked by At

I'm trying to automate the process to replace existing table in TablePress Wordpress plugin with Python and Selenium webdriver, but I stuck at the point where I should choose the existing table from a dropdown. How can I select the "ID bcwd_stock_auto: (no name)" option of this dropdown?

<td class="column-2">
    <div class="jdropdown jdropdown-default">
        <div class="jdropdown-container-header">
        <input class="jdropdown-header jss_object" type="text" autocomplete="off" placeholder="— Select or type —"></div>
        <div class="jdropdown-container" style="min-width: 744.031px;"><div class="jdropdown-close">Done</div>
        <div class="jdropdown-content">
        <div class="jdropdown-item">
            <div class="jdropdown-description">— Select or type —</div></div>
            <div class="jdropdown-item">
            <div class="jdropdown-description">ID bcwd_current_stock: (no name)</div>
            </div>
            <div class="jdropdown-item">
            <div class="jdropdown-description">ID bcwd_stock_auto: (no name)</div>
            </div>
            </div>
        </div>
    <div class="jdropdown-backdrop"></div>
        </div>
<select id="tables-import-existing-table" name="import[existing_table]" style="display: none;">
    <option value="">— Select or type —</option>
    <option value="bcwd_current_stock">ID bcwd_current_stock: (no name)</option>
    <option value="bcwd_stock_auto">ID bcwd_stock_auto: (no name)</option>
</select>
</td>

I tried:

select = Select(driver.find_element_by_id('tables-import-existing-table'))
select.select_by_value('bcwd_stock_auto')

And this:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select//option[contains(., 'bcwd_stock_auto')]"))).click()

But with no luck. Any idea how should I do it?

1

There are 1 answers

0
Gabor On

I found a solution that worked:

dropd = driver.find_element(By.CSS_SELECTOR, 'div.jdropdown-container-header input.jdropdown-header.jss_object')
dropd.click()
dropd.send_keys(Keys.DOWN, Keys.DOWN, Keys.DOWN, Keys.RETURN)