How can I click the Attach File button in Gmail with Python + Selenium?

Viewed 144

I try to click on the button that appears in gmail with selenium, but I have not been able to click on it. enter image description here

This is the code I have:

driver.find_element_by_css_selector('div.aaA').send_keys(r"C:\Users\micomputer\Desktop\perritos\dog.png")

When running it, I get an error that the button cannot be found. Somebody can help me? Thank you in advance for your support. Greetings!

1 Answers

To upload a file with Selenium by sending the uploading file path, you should use this element locator //input[@type="file"]
So instead of

driver.find_element_by_css_selector('div.aaA').send_keys(r"C:\Users\micomputer\Desktop\perritos\dog.png")

Please try

driver.find_element_by_xpath('//input[@type="file"]').send_keys(r"C:\Users\micomputer\Desktop\perritos\dog.png")
Related