1

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?

1 Answer 1

1

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:\webdrivers\chromedriver.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:\webdrivers\chromedriver.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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You very much for your solution. PS. As far as I know in Selenium 4.6.0 creating Service object is not needed anymore (exe file is not necessary)
Actually I code with Java and there is no such Service there, but I will take a look. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.