Automate a cell editor drop down using selenium

Viewed 47

I am trying to automate the drop down in image ag grid drop down 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 is the URL and Select editor is the drop down. Can someone help me to automate this using selenium?

1 Answers

This is the working python code, you can try to convert it to java. Notice that since the cells are contained in a iframe webelement, in order to select a cell with find_element() you have first to switch to the iframe, as shown in the code.

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

enter image description here

Related