Changing a value in a table with selenium

Viewed 222

I try to put in new values in the rows of the table on this website using selenium. My approach so far:

URL = "https://sigmazone.com/catapult-grid/"

browser = webdriver.Firefox("/usr/lib/firefox")
browser.get(URL)

# enter number of rows = 1
num_rows = browser.find_element_by_id("noOfRows")
num_rows.clear()
num_rows.send_keys("1")

# updating number of rows
update_btn = browser.find_element_by_id("updateNoOfRows")
update_btn.click()


cells = browser.find_elements_by_class_name("htRight")
# skipping the first entry in cells because it contains a div with the whole table
for cell in cells[1:]:
    cell.click()
    cell.send_keys("150")

Trying this gave me Message: Element <td class="htRight current highlight"> is not reachable by keyboard from the send_keys(150) method.

UPDATE:

This is how I solved it. Resetting the action chain is essential. Otherwise all previous actions are performed every time you do action.perform()

def set_val(selected_cell, value):
    action.move_to_element(selected_cell).double_click()  # select the cell
    action.send_keys(Keys.BACKSPACE, Keys.BACKSPACE, Keys.BACKSPACE)  # deleting previous numbers
    action.send_keys(str(value))  # send the value
    action.perform()  # perform the action chain
    action.reset_actions()  # this alone did not reset the action chain completely
    for device in action.w3c_actions.devices:
        device.clear_actions()
2 Answers

You have to let page fully loaded before accessing elements there.
Please try this:

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

URL = "https://sigmazone.com/catapult-grid/"

browser = webdriver.Firefox("/usr/lib/firefox")
wait = WebDriverWait(browser, 20)

browser.get(URL)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "td.htRight")))

cells = browser.find_elements_by_class_name("htRight")
# skipping the first entry in cells because it contains a div with the whole table
for cell in cells[1:]:
    cell.click()
    cell.send_keys("150")

UPD
In case cell.send_keys("150") doesn't work try using actions:

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


URL = "https://sigmazone.com/catapult-grid/"

browser = webdriver.Firefox("/usr/lib/firefox")
wait = WebDriverWait(browser, 20)
actions = ActionChains(browser)

browser.get(URL)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "td.htRight")))

cells = browser.find_elements_by_class_name("htRight")
# skipping the first entry in cells because it contains a div with the whole table
for cell in cells[1:]:
    cell.click()
    actions.send_keys('150').perform()

There is no need to use execute_script. Please use selenium native actions.

The thing is you'd have to double click on a cell and then you can enter your desired number.

cells = driver.find_elements_by_class_name("htRight")
# skipping the first entry in cells because it contains a div with the whole table
action = ActionChains(driver)
for cell in cells[1:]:
    action.move_to_element(cell).double_click().pause(2).send_keys('150').perform()

Imports :

from selenium.webdriver.common.action_chains import ActionChains
Related