As title said, I am working on downloading file in Thai government website. I tried to go back to the old page, delete the old code (hscode) which I had already downloaded and refilled with another one. However, I stuck with sending a `.send_keys(Keys.BACKSPACE)'. I already tried both implicit and explicit wait, but it did not work in my case. Also, I have tried to research on this with the conclusion that "the element is no longer in the DOM or it changed" but no solution was attached. Any ideas or solutions to send that Backsplace key would be very appreciated. Thanks.
What I have tried so far is per below code:
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)
An error message I got is:
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)
EDIT: Thank you for @DebanjanB who suggests me to follow the solution in StaleElementReference Exception in PageFactory. After I had followed that, it still didn't work with my case (I tried by defining "main_window" and "hscode" again after driver.close()
. It gave be this error message.
NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"q_hsList"}
Please kindly suggest me other ideas or solutions. Thank for your time.
The reason that the hscode isn't found the second time you visit the main window is that this hscode is within a frame named 'data'. So after switching back to the main window you also directly have to switch to this frame:
driver.switch_to.window(main_window)
driver.switch_to.frame("data")