如何使用 div tag/class?.selenium 从下拉列表中 select 一个选项

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

我已经尝试了所有可能的事情。此外,尝试搜索尝试不同的组合和变化。我可以点击打开下拉菜单的元素。但我无法在其中 select 选择一个选项。我尝试了动作、sendkeys、keys.down/enter 和多种东西。但是,它没有帮助。这是我唯一坚持的事情。

//select办公室

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- 在下图中。

您应枚举所有选项,然后单击所需选项。看看this.

编辑

The easiest way that I have found was to do something along the lines of:

el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'The Options I Am Looking For':
        option.click() # select() in earlier versions of webdriver
        break

下面的代码对我有用

WebElement selectMyElement = driver.findElement(this.getObject(By.Id("您的下拉列表的 ID"))); selectMyElement.click();

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