Selecting jquery dropdown list using XPATH

Question:

Actually I am doing tasks from https://demo.seleniumeasy.com/jquery-dropdown-search-demo.html. But I found a problem – I can’t find any element on this page using XPATH. For example I want to find "Select Country" using driver.find_element and XPATH:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://demo.seleniumeasy.com/jquery-dropdown-search-demo.html")

jquery_drop_list = driver.find_element(by=By.XPATH, value="//span[@class='select2-selection select2-selection--single']")

#jquery_drop_list = driver.find_element(by=By.XPATH, value="//span[@class='select2 select2-#container select2-container--default select2-container--above select2-container--focus']")

#jquery_drop_list = driver.find_element(by=By.XPATH, value="//span[@class='select2-hidden-#accessible']")

print(jquery_drop_list)

But none of the above searches works.

Could you advise me on what a proper selector should look like for similar problems? Maybe XPATH selector is not a good choice here?

Asked By: Karol

||

Answers:

There is a Select block here.
You need to utilize Selenium Select object for that.
This code is selecting Denmark:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

url = "https://demo.seleniumeasy.com/jquery-dropdown-search-demo.html"

driver.get(url)

select_country = Select(wait.until(EC.element_to_be_clickable((By.ID, 'country'))))
select_country.select_by_value("Denmark")

But if you still want to open that drop down with regular click it is possible too. This XPath works:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

url = "https://demo.seleniumeasy.com/jquery-dropdown-search-demo.html"

driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//span[@aria-labelledby='select2-country-container']"))).click()

Generally, XPath is most powerful way to select web elements with selenium.
Some people just not familiar with it 🙂
And sometimes some XPaths not properly supported by some webdrivers, but if you are using Chromedriver you will see no problems with XPaths.

Answered By: Prophet