Home > Back-end >  Changing a value in a table with selenium
Changing a value in a table with selenium

Time:10-12

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.

CodePudding user response:

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

CodePudding user response:

You can use JavascriptExecutor when WebDriver methods are not working.

cells = browser.find_elements_by_class_name("htRight")
browser.execute_script("arguments[0].value='150', cells);

Parameterized

 browser.execute_script("arguments[0].value="   "'"   fieldValue   "'", cells);

CodePudding user response:

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