正如标题所说,我正在泰国政府网站上下载文件。 我试图回到旧页面,删除我已经下载的旧代码(hscode)并重新填充另一个。但是,我坚持发送`.send_keys(Keys.BACKSPACE)'。我已经尝试了隐式和显式等待,但它在我的情况下不起作用。此外,我试图对此进行研究,结论是“元素不再存在于DOM中或者它已经改变”,但没有附加任何解决方案。发送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"}
请建议我其他想法或解决方案。谢谢你的时间。
答案 0 :(得分:0)
第二次访问主窗口时未找到hscode的原因是此hscode位于名为“data”的帧中。因此,在切换回主窗口后,您还必须直接切换到此框架:
driver.switch_to.window(main_window)
driver.switch_to.frame("data")