使用 selenium 自动化单元格编辑器下拉菜单

Automate a cell editor drop down using selenium

我正在尝试使图像中的下拉菜单自动化 and I used javascript and Javascript executor to change the value. I was able to change the value but the value is not set once I click outside the drop down. Used document.querySelector("div[ref='eDisplayField']").innerText='Red'but the value is not changed completely. Locating the values inside the drop down also is not possible as it does not show anything on inspecting the values. https://www.ag-grid.com/angular-data-grid/provided-cell-editors/#select-cell-editor 是 URL,Select 编辑器是下拉菜单。 有人可以帮助我使用 selenium 实现自动化吗?

这是有效的 python 代码,您可以尝试将其转换为 java。请注意,由于单元格包含在 iframe 网络元素中,为了 select 具有 find_element() 的单元格,您必须首先切换到 iframe,如代码所示。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(service=Service(your_chromedriver_path))
driver.get('https://www.ag-grid.com/angular-data-grid/provided-cell-editors/#select-cell-editor')

# wait for cookie banner and click reject
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#onetrust-reject-all-handler'))).click()

# wait for iframe and switch to it
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title=editors]")))

# scroll to table
table = driver.find_element(By.XPATH, "//body")
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', table)

# wait for thrid cell in third line to be visible
el = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div[comp-id="95"]')))
time.sleep(2)
actions = ActionChains(driver)
actions.double_click(el).perform()
# open drop down menu
el.click()
# select a color by simulating arrow up on keyboard and press enter
actions.send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()