Selecting from Div Class Dropdown - Selenium

how to select element of div dropdown in selenium webdriver

As Dmitri T mentioned, always try to use relative XPath instead of absolute XPath.

Your code is not working most probably for the absolute XPath is invalid.

Also, use explicit waits instead of implicit waits. The list is present in the DOM always but it is only visible and clickable when you click on the dropdown field.

Try this:

driver.get("https://goodprofbadprof.com/search-school");
driver.findElement(By.xpath("//div[@class='form-group']/label[contains(text(),'Search By')]/following-sibling::div")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@role='menu']/li[contains(text(),'State')]"))).click();

How to select a option from drop down with div tag/class?.selenium

Below code worked for me

WebElement selectMyElement = driver.findElement(this.getObject(By.Id("Id of Your DropDown")));
selectMyElement.click();

Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN, Keys.ENTER)).perform();

selecting a drop down item from a <div> menu selenium python

Induce WebDriverWait() and element_to_be_clickable() and click on the All reviews div element to open up the dropdown menu and then select the items based on text.

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

driver=webdriver.Chrome()
driver.get("https://www.google.com/maps/place/Hilton+London+Bankside/@51.5056536,-0.1033145,17z/data=!3m1!4b1!4m10!3m9!1s0x487604af6af74cc7:0x6c4cb3cbe03e95bf!5m2!4m1!1i2!8m2!3d51.5056536!4d-0.1011258!9m1!1b1")
#Dropdown text provide here
selectItem='Agoda'
#First click on the All reviews element to open up the dorpdown element
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[aria-label='All reviews']"))).click()
#Select item from menu dropdown by text
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@role='menuitem']//div[text()='"+ selectItem +"']"))).click()

Browser snapshot: after execution

enter image description here

How to handle dropdown of type div instead of select in selenium/python

Try to pass the correct xpath

driver.find_element_by_xpath("//div/span[@id='react-select-2--value-item']").click()

How to Select Drop down developed with Div Tag in Selenium

Finally, I got answer.  
It worked with javascriptExecutor.

First clicked on dropdown and then value using javascriptExecutor
jse = (JavascriptExecutor)driver;
//click on dropdown
jse.executeScript("arguments[0].click();",profPrefMedium);
//click on the value
dValue = data.get("PROFILE PREFERRED MEDIUM");

if(dValue.equalsIgnoreCase("Email"))
{
jse.executeScript("arguments[0].click();",profPrefMediumEmail);

}

Thanks all for your help.


Related Topics



Leave a reply



Submit