Python Selenium – how to click and pick from the dropdown list items without select tag

Issue

I’m new to Python and Selenium, try to automate filling out the form and get the game gift.

Here is a html, and it uses list items instead of select tag.

<tr>
                <th>Server Name</th>
                <td>
                    <!-- toDev liのdata-value属性に設定して頂いた値が下のhiddenに入ります。 --> <input
                    type="hidden" name="server" class="js-selected-server">
                    <ul class="serialForm__select js-select-form" data-type="server">
                        <li><span class="js-selected-text">Select Server</span>
                            <div class="serialForm__select--btn">
                                <img class="iconArrow" data-type="primary"
                                    src="/img/nav/nav_arrow01.svg"> <img class="iconArrow"
                                    data-type="secondary" src="/img/nav/nav_arrow02.svg">
                            </div></li>
                        <li data-value="1">Orchard(KOR)</li>
                        <li data-value="2">Dynasty(CHN)</li>
                        <li data-value="3">Warlord(SEA)</li>
                        <li data-value="4">Conquest(US)</li>
                        <li data-value="5">Invincible(JP)</li>
                        <li data-value="6">Inferno(TW)</li>
                        <li data-value="7">Ascension(KOR)</li>
                    </ul>
                </td>
            </tr>
            <tr>

Script is able to click the dropdown menu, but not able to pick any of the listed items.
enter image description here

I tried the following and none of those works.

# dropdown = Select(select_server)
# dropdown.select_by_visible_text('Conquest(US)')
# dropdown.select_by_index('4')

My code:

from selenium import webdriver
from selenium.webdriver.support.select import Select

import time

edge_options = {
    "executable_path": "/edgedriver_mac64/msedgedriver",
    # ofcourse change path to driver you downloaded.
    "capabilities": {
        "platformName": 'mac os x',  # I get this from Chrome driver's capabilities
        # "os" : "OS X", # also ok.
    }
}
browser = webdriver.Edge(**edge_options)
browser.get('https://kstory.hangame.com/en/index')

time.sleep(2)

select_server = browser.find_element_by_css_selector(
    "span[class='js-selected-text']")

time.sleep(2)
select_server.click()


## below are still testing, not working yet 
select_server.send_keys('Conquest(US)')    
# dropdown = Select(select_server)
# dropdown.select_by_visible_text('Conquest(US)')
# dropdown.select_by_index('4')

Any help is appreciated, thanks.

Solution

You can try this

driver.find_element_by_xpath("//ul[@data-type='server']/li[text()='Conquest(US)']").click()

Answered By – itronic1990

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published