python - 使用 selenium 更改表中的值

标签 python selenium

我尝试在 this 上的表行中输入新值使用 Selenium 的网站。到目前为止我的方法:

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

尝试这个给了我 Message: Element <td class="htRight current highlight"> is not reachable by keyboard来自send_keys(150)方法。

更新:

我就是这样解决的。重置行动链至关重要。否则,每次执行 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()

最佳答案

您必须先让页面完全加载,然后才能访问其中的元素。
请尝试这个:

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
如果 cell.send_keys("150") 不起作用,请尝试使用 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()

关于python - 使用 selenium 更改表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69524805/

相关文章:

python - 将两列合并为一列

selenium - 在 docker 环境中使用 selenium 进行 Behat 测试

python - 使用文本 selenium beautifulsoup python 获取标签

python - selenium python webdriver 有没有办法断言所有复选框都已选中或未选中?

java - Selenium Webdriver 和 Java。元素在点 (x, y) 处不可点击。其他元素会收到点击

python - Elasticsearch 如何查询高于 x 的 ID 字段

python - 如何从外部命令结合管道获取输出

c++ - 如何让 Visual Express 2010 找到我的 python.h 头文件?

python - 具有多个条件的 Numpy "where"

ios - 如何以编程方式在 Safari 中启用 "Allow Remote Automation"