0

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
ShridharK
  • 365
  • 2
  • 14
  • Let me know if anyone has any questions with regards to the code. – ShridharK Jun 01 '21 at 11:59
  • The code for changing name is from this question which is having 1st answer: https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading – ShridharK Jun 01 '21 at 12:06
  • I'm getting this error: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document – ShridharK Jun 01 '21 at 12:24
  • option.text in the filepath doesnt look right. When you print option.text what is the output? Did you try changing it to mySelect.first_selected_option? – itronic1990 Jun 01 '21 at 12:46
  • If I try to print option.text outside of the try and except block, it works completely fine. Above the index = index + 1 code, I tried that and it started printing all the option names – ShridharK Jun 01 '21 at 12:51
  • try to print option.text before shutil.move(filename,os.path.join("C:\\Users\\xyz\\Downloads",'"' + option.text + '.pdf"')). Also did you try appending "r" before option.text as filepath? – itronic1990 Jun 01 '21 at 13:02
  • Sorry, I forgot to give an update, I've found a way out. Essentially, I'm running a for loop before the one in my code where I'm appending all the option.text values in an emtpy list. And then I'm using that list to get the name of the file while downloading. It's not throwing any error as such but the new problem I've noticed is that some files get renamed properly while others are renaming just the previously renamed file, if you get what I mean. So I'm trying to find the problem here and trying to understand what's happening. – ShridharK Jun 01 '21 at 13:09
  • I've added my answer – ShridharK Jun 01 '21 at 13:26

1 Answers1

0

Here's how I did it.

I first used a small for loop where I appended option.text values to a new list:

index=-1
names = []    
for option in mySelect.options:
    names.append(option.text)
    print('names is: ',names)

And then inside try and except, I added this:

filename = max(["C:\\Users\\parnal.patil\\Downloads" + "\\" + f for f in os.listdir("C:\\Users\\parnal.patil\\Downloads")],key=os.path.getctime)
        shutil.move(filename,os.path.join("C:\\Users\\parnal.patil\\Downloads",'2021Q1_' + str(names[index]) + '.pdf'))

The rest of the code is as is. I also increased the time.sleep duration in a few places but those are minor changes.

ShridharK
  • 365
  • 2
  • 14