Mohammad Izaan
Mohammad Izaan

Reputation: 45

How to upload multiple/single file using selenium python?

I am stuck in a project in which I am trying to upload file to a website. This is www.coursehero.com.

Actually I am trying to make a script to upload the document automatically on the website I mentioned, it is an educational website.

Further the website may show captcha sometimes, but if you load the cookies by adding your data directory then it will not usually asks to solve captcha. You may need to login manually first time in selenium browser by extending time limit.

Here is the direct link of the uploading page. https://www.coursehero.com/upload/?__chid=61cbbae8-cd73-4877-b30d-7826dfd0833e

You can use the following id and pwd to login:

[email protected]
izaan100

Here is my code:

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
import csv
import pandas as pd
import sys
import random
import os
#import autoit


options = Options()
#options.add_argument('--proxy-server=%s' % Ip)
options.add_argument('--user-agent={Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36}')
options.add_argument("user-data-dir=C:\\Users\\name\\AppData\\Local\\Google\\Chrome\\User Data\\selenium1")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--remote-debugging-port=9222")
options.add_argument('--no-sandbox')
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("disable-infobars")
path=r'C:\Users\name\Downloads\chromedriver.exe'
driver = webdriver.Chrome(path, options=options)
driver.get('https://www.coursehero.com/login/?ref=login') 
time.sleep(6)






try:
    driver.find_element_by_xpath('//*[@id="email-address-field"]').send_keys('[email protected]')
    driver.find_element_by_xpath('//*[@id="password-field"]').send_keys('izaan100')
    driver.find_element_by_xpath('//*[@id="login-submit-field"]').click()
    time.sleep(8)
except:
    pass



driver.get('https://www.coursehero.com/upload/?__chid=61cbbae8-cd73-4877-b30d-7826dfd0833e')
time.sleep(12)

upload=driver.find_element_by_xpath('//*[@id="noFrictionUploaderApp"]/div/div/div[2]/div/div/div/div/div/div/div/div/div[2]/div/span/button/span')
upload.send_keys('C:\\Users\\name\\Desktop\\files\\Example.docx')
time.sleep(10)

What I want is to upload the file, but I am getting the error:

Error: Element not found

The code is not working, I tried other stuff like:

upload.send_keys(r'C:\Users\name\Desktop\files\Example.docx')

but not worked, I searched on google and stack but this is the only way I found to upload file in selenium python.

Can anyone guide me and help me?

Upvotes: 0

Views: 368

Answers (2)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

You need to find the input tag then sendkeys, you're putting the link_to_file to span tag.

You could try this xpath //input[@type='file']

enter image description here

Upvotes: 1

mr_mooo_cow
mr_mooo_cow

Reputation: 1128

I haven't run your code yet, but I notice on the website you have to actually click the button to open the file dialogue, which you do not do. Have you tried including the .click() selenium command after you select the xpath?

Upvotes: 0

Related Questions