Home > database >  how to select something from a dropdown menu that has no select element ? python selenium
how to select something from a dropdown menu that has no select element ? python selenium

Time:07-20

hello im practicing selenium on a practice forum this the link for it : click here

if visit the page and inspect element on the dropdown menu for state and city you will find it consists of only div element i tried doing this but didnt work obviously :

dropdown = Select(d.find_element("xpath",'//*[@id="state"]'))
dropdown.select_by_index(0)

this the error message :

Select only works on <select> elements, not on <div>

can someone show how to loop through the value of the menu or is there any other solution ?

CodePudding user response:

This code is working

search_url = 'https://demoqa.com/automation-practice-form'

driver = webdriver.Chrome(options = options, executable_path= os.path.join(os.environ['USERPROFILE'],"Desktop")   f'\\Python\\available Tender\\chromedriver\\chromedriver.exe')

driver.get(search_url)

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

element1 = WebDriverWait(driver, 4).until(EC.presence_of_element_located((By.XPATH, f"//div[@id='adplus-anchor']")))
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element1)

element2 = WebDriverWait(driver, 4).until(EC.presence_of_element_located((By.XPATH, f"//div[@id='currentAddress-wrapper']")))
driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element2)

driver.find_element(By.XPATH, '//*[@id="state"]/div/div[2]/div').click()
e1 = WebDriverWait(driver, 4).until(EC.presence_of_element_located((By.XPATH, f"//div[contains(@class,'menu')]")))
e1.find_element(By.XPATH, "//div[contains(text(),'NCR')]").click()
  • Related