Python 无法发送退格键(编辑)

Python could not send Backspace keys (EDIT)

如标题所述,我正在泰国政府网站上下载文件。 我试图回到旧页面,删除我已经下载并重新填充另一个代码的旧代码(hscode)。然而,我坚持发送`.send_keys(Keys.BACKSPACE)'。我已经尝试过隐式和显式等待,但它在我的情况下不起作用。另外,我试图对此进行研究,得出的结论是 "the element is no longer in the DOM or it changed" 但没有附加解决方案。发送该 Backsplace 键的任何想法或解决方案将不胜感激。谢谢。

到目前为止我已经尝试过以下代码:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys

kobs =["8507", "6910"]
for kob in kobs:
    ChromeOptions = webdriver.ChromeOptions()
    prefs = {"download.default_directory":"C:/Users/Kob/Desktop/Testnaja"}
    ChromeOptions.add_experimental_option("prefs", prefs)
    ChromePath = "C:/Users/Kob/Desktop/Python projects/Chrome webdriver/Chromedriver.exe"
    
    driver = webdriver.Chrome(executable_path=ChromePath, chrome_options=ChromeOptions)
    driver.get("http://www2.ops3.moc.go.th/")
    
    main_window = driver.window_handles[0]
    new_window1 = driver.window_handles[1]
    new_window2 = driver.window_handles[2]


    Export = driver.find_element_by_link_text("EXPORT")
    Export.click()

    driver.switch_to.window(new_window1)
    driver.close()
    driver.switch_to.window(new_window2)
    driver.close()
    driver.switch_to.window(main_window)

    driver.switch_to.frame("data")
    driver.implicitly_wait(5)

    Commodity = driver.find_element_by_link_text("Commodity")
    Commodity.click()

    Year = Select(driver.find_element_by_name("q_Year"));
    Month = Select(driver.find_element_by_name("q_Month"));
    Currency = Select(driver.find_element_by_name("q_currency"));

    Year.select_by_index("0")
    Month.select_by_index("1")
    Currency.select_by_index("1")


    hscode = driver.find_element_by_name("q_hsList")
    hscode.send_keys(kob)


    driver.execute_script("doReport()")

    new_window3 = driver.window_handles[1]
    driver.switch_to_window(new_window3)
    driver.find_element_by_id("exportdlgImage").click()
    new_window4 = driver.window_handles[2]
    driver.switch_to_window(new_window4)

    Format = Select(driver.find_element_by_name("exportformat"))
    Format.select_by_index("4")

    All = driver.find_element_by_id("radio1")
    All.click()

    Button = driver.find_element_by_xpath("/html/body/form/table/tbody/tr[10]/td/input")
    Button.click()

    WebDriverWait(driver, 20)

    driver.switch_to_window(new_window3)
    driver.close()

    driver.switch_to_window(main_window)

    hscode.click()
    hscode.send_keys(Keys.BACKSPACE)

我收到的错误消息是:

StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.3.9600 x86_64)

编辑: 感谢@DebanjanB 建议我遵循 StaleElementReference Exception in PageFactory 中的解决方案。在我遵循之后,它仍然不适用于我的情况(我尝试在 driver.close() 之后再次定义 "main_window" 和 "hscode"。它给出了这个错误消息。

NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"q_hsList"}

请向我提出其他想法或解决方案。谢谢你的时间。

您第二次访问主 window 时找不到 hscode 的原因是此 hscode 在名为 'data' 的框架内。所以切换回主window后你也直接切换到这个框架:

driver.switch_to.window(main_window)
driver.switch_to.frame("data")