PYTHON - Tkinter & Selenium file conversion automation: send_keys function returns no attribute & challenge with finding elements

Viewed 123

I'm in the process of trying to automate file conversions from PDF to Excel files. I'm going to be using a simple website (pdftoexcel.com) to do the file conversion.

I first wanted to use my newly acquired Tkinter skills as a way to browse the file as the first function of the code:

def open_file():

root.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("PDF files","*PDF"),("jpeg files","*.jpg"),("all files","*.*"), ))
print (root.filename)
filelocation = (root.filename)
print (filelocation)

root = Tk()

button1 = Button (root, text = "Browse", command=open_file, fg = "red",bg = "white")
img = PhotoImage (location/to/file/picture.png)
button1.config (image=img)
button1.pack()
root.geometry("500x50")
root.mainloop()

Once I got my browsing function to work, I started writing the code using Selenium to navigate through Google Chrome:

chromedriver = "path/to/chromedriver.exe"

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.pdftoexcel.com/")
El = driver.find_element_by_name("Filedata").click()
El.send_keys(filelocation)
driver.find_element_by_link_text("/fetch.php?id=361caa98-17b8-11e9-8f58-0cc47a792c0a").click()

time.sleep(3)
driver.quit()

Everything seems to go smoothly until I have to select the file on the website. I want to be able to use the file path I obtained back when I browsed my file with Tkinter. I've seen people using the

send_keys()

function, where they would put in the file path (that is why mine has the filelocation variable)

El.send_keys(filelocation)

However, I keep receiving this error:

AttributeError: 'NoneType' object has no attribute 'send_keys'

Also, from the website, I am trying to obtain both the Upload and Download elements in order to automatically upload and download. The upload seems to be working fine but no matter what element I try to use for the download (Xpath, class name, link text), I keep receiving these errors:

 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"/fetch.php?id=361caa98-17b8-11e9-8f58-0cc47a792c0a"}
 (Session info: chrome=71.0.3578.98)

I've scoured the web and I cannot seem to find a solution that would suit the problem I'm having.

Some help with my code would be much appreciated!

1 Answers

the E1 here is the return value of function click(), of course it's a None value

driver.get("https://www.pdftoexcel.com/")
E1 = driver.find_element_by_name("Filedata").click()
E1.send_keys(filelocation)

correct code should be like this :

driver.get("https://www.pdftoexcel.com/")
E1 = driver.find_element_by_name("Filedata")
E1.click()
E1.send_keys(filelocation)

in addition, as suggestion, if you wait for web browser, not simplely use time.sleep, use selenium time wait module instead.

from selenium.webdriver.support.wait import WebDriverWait
Related