我嘗試使用硒在此網站上的表格行中放入新值。到目前為止我的方法:
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 > 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()
uj5u.com熱心網友回復:
在訪問那里的元素之前,您必須讓頁面完全加載。
請試試這個:
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()
uj5u.com熱心網友回復:
沒有必要使用execute_script. 請使用硒本機操作。
問題是您必須雙擊一個單元格,然后才能輸入所需的數字。
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()
進口:
from selenium.webdriver.common.action_chains import ActionChains
uj5u.com熱心網友回復:
您可以JavascriptExecutor在WebDriver方法不起作用時使用。
cells = browser.find_elements_by_class_name("htRight")
browser.execute_script("arguments[0].value='150', cells);
引數化
browser.execute_script("arguments[0].value=" "'" fieldValue "'", cells);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313852.html
