Search code examples
python-3.xseleniumxpathcss-selectorswebdriverwait

How to pick an answer out of the box using Selenium Python


I want to make selenium select a choise from box

<li class="country" data-dial-code="1" data-country-code="us"><div class="flag-box"><div class="iti-flag us"></div></div><span class="country-name">United States</span><span class="dial-code">+ 1</span></li>

I want to change the data-dial-code can I do this? and thanks


Solution

  • The click on the element:

    <li class="country" data-dial-code="1" data-country-code="us">
    

    You need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.country[data-country-code='us'] div.us"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='country' and @data-country-code='us'][.//span[text()='United States']]"))).click()
      
    • Note: You have to add the following imports :

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