Search code examples
javahtmlseleniumtagsdropdown

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


I have tried all the possible things. also, tried searching over tried different combinations and variations. I can get the element clicked which opens the dropdown. but i am not able to select an option in it. I tried with actions, sendkeys, keys.down/enter and multiple things as well. however, it didn't help. That's the only thing I am stuck on.

//selecting office

1.driver.findElement(By.id("DG5QEPn")).click();

Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.xpath("//*[@id="DG5QEPn"]/div/div/div1/div1"))).click();

driver.findElement(By.id("DG5QEPn")).click();

driver.findElement(By.id("DG5QEPn")).sendKeys("RTP HQ"); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Down); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Enter);

Html- In the below image.

enter image description here


Solution

  • 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();