我試圖使用 selenium 從“只讀”輸入欄位中清除文本欄位。
這部分代碼,雖然我沒有收到錯誤,但它不起作用:
from selenium.webdriver.common.keys import Keys
enddate = driver.find_element_by_xpath('//*[@id="end"]')
enddate.send_keys(" ")
time.sleep(3)
我也嘗試通過元素 id 來做到這一點,使用 clear() 向欄位發送空字串,但它不起作用。這是我試圖在 UI 上清除的欄位(我希望清除 enddate 欄位)
對于測驗,如果我嘗試通過修改開發人員工具上的 HTML 元素來洗掉該值,那么它可以作業,但它不是自動化的并且不使用 selenium。
關于我如何做到這一點的任何建議?謝謝
uj5u.com熱心網友回復:
您需要先洗掉該readonly屬性:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
end_date = driver.find_element_by_xpath('//*[@id="end"]')
# Remove the attribute first:
driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="end"]'))))
end_date.send_keys(" ")
time.sleep(3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/408592.html
標籤:
