0

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.

Kob
  • 147
  • 1
  • 11
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Apr 11 '18 at 05:08
  • Thanks for your response @DebanjanB. But, I still got the error message per below. Noted: I tried by 'defining main_window' and 'hscode' again after 'driver.close()' NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"q_hsList"} – Kob Apr 11 '18 at 06:04
  • I Struggled with your code for more then a hour but until didn't solve the problem. – Frank Apr 11 '18 at 08:07
  • Thank you for your hard effort @Frank. And that why I need to post this issue and hope some potential guru to solve this. – Kob Apr 11 '18 at 09:53
  • Did you try my solution? – Frank Apr 12 '18 at 17:06
  • Hi @Frank . Sorry for late response. I have just tries your solution and yes, it works very well. Thanks a lot for your help. You save my life twice! :D. Would you mind if I asked for your email in case I need to discuss a specific issue? – Kob Apr 12 '18 at 18:34
  • No problem. frankpeetersamsterdam@gmail.com – Frank Apr 12 '18 at 18:35

1 Answers1

0

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")  
Frank
  • 831
  • 1
  • 11
  • 23
  • Hi @Frank. Sorry for late response. I have just tries your solution and yes, it works very well. Thanks a lot for your help. You save my life twice! :D – Kob Apr 12 '18 at 18:29