I'm looping over options and based on the option.text value, I'm trying to rename my PDF file. However, I'm facing an error.
Here is the code:
mySelect = Select(driver.find_element_by_id("childContextDDL"))
index=-1
for option in mySelect.options:
time.sleep(1)
index = index + 1
try:
dropdown = driver.find_element_by_id('lnkChangeChild')
dropdown.click()
dropdown1 = driver.find_element_by_class_name('k-select')
dropdown1.click()
driver.execute_script("document.getElementById('childContextDDL').style.display = 'block';")
mySelect1 = Select(driver.find_element_by_id("childContextDDL"))
mySelect1.select_by_index(index)
randomClick = driver.find_element_by_id('dcf-user-info')
randomClick.click()
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
time.sleep(1)
driver.close()
driver.switch_to.window(driver.window_handles[0])
except:
mySelect.select_by_index(index)
randomClick = driver.find_element_by_id('dcf-user-info')
randomClick.click()
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
filename = max(["C:\\Users\\xyz\\Downloads" + "\\" + f for f in os.listdir("C:\\Users\\xyz\\Downloads")],key=os.path.getctime)
time.sleep(1)
shutil.move(filename,os.path.join("C:\\Users\\xyz\\Downloads",'"' + option.text + '.pdf"'))
time.sleep(1)
driver.close()
driver.switch_to.window(driver.window_handles[0])
I'm facing an error on this line:
shutil.move(filename,os.path.join("C:\\Users\\xyz\\Downloads",'"' + option.text + '.pdf"'))
If I change that line to this, it doesn't throw an error:
shutil.move(filename,os.path.join("C:\\Users\\xyz\\Downloads",r"ScoreCard.pdf"))
But I want to dynamically change the name of the PDF file based on the option selected.
For reference, here are the options:
<select id="childContextDDL" data-filter="contains" data-role="dropdownlist" data-template="dcf-context-picker" data-value-field="Value" data-text-field="DisplayText" data-bind="events: { change: childContextListChange }, source: childContextList.ChildContextList" style="display: block;">
<option value="1">NATION</option>
<option value="12">ATLANTIC</option>
<option value="16">CHARLOTTE, NC
So for Nation, I want rename file as Nation.pdf while for Atlantic it will be, Atlantic.pdf, etc.
Let me know where I'm going wrong.
This is the error:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document