ASH
ASH

Reputation: 20362

Trying to upload file to web browser using Selenium

I am trying to automate the take of uploading a CSV file to a web browser. If you click the button, a windows dialog box opens, you select the file, and it is uploaded as expected. I'm trying to automate this small task.

The HTML looks like this:

<input name="csvFile" id="csvFile" class="ignore-inline-attach" type="file">

The code that I'm trying to get working looks like this:

import requests
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from webbot import Browser

driver = webdriver.Chrome(executable_path= r'C:/Users/ryans/OneDrive/Desktop/Briefcase/Python Scripts/chromedriver.exe')

web = Browser()
url = 'https://web_site'
web.go_to(url)

# 1st validation
web.type('email' , into='username')
web.click('Continue')

# 2nd validation
web.type('email' , into='username')
web.click('Next')

# password
web.type('pass' , into='password')
web.click('Next')

# Now you are logged in!!
url = 'https://web_page'
web.go_to(url)

#################################

# upload CSV Source File:
WebElement = webdriver.find_element_by_class_name('ignore-inline-attach')
WebElement.sendKeys("C:/my_path/test.csv");

#################################

When I run this code, I get the following error message:

AttributeError: module 'selenium.webdriver' has no attribute 'findElement'

I found a few sample scripts online, and tried several ideas, but I couldn't get anything working. The two lines of code that I posted above seems like the best way to move forward, but I can't seem to get this working. Does anyone here have any idea how to do this? Thanks!

dir(webdriver)
# Result:
['ActionChains',
 'Android',
 'BlackBerry',
 'Chrome',
 'ChromeOptions',
 'DesiredCapabilities',
 'Edge',
 'Firefox',
 'FirefoxOptions',
 'FirefoxProfile',
 'Ie',
 'IeOptions',
 'Opera',
 'PhantomJS',
 'Proxy',
 'Remote',
 'Safari',
 'TouchActions',
 'WebKitGTK',
 'WebKitGTKOptions',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'android',
 'blackberry',
 'chrome',
 'common',
 'edge',
 'firefox',
 'ie',
 'opera',
 'phantomjs',
 'remote',
 'safari',
 'support',
 'webkitgtk']

Upvotes: 1

Views: 1022

Answers (1)

Federico Rubbi
Federico Rubbi

Reputation: 734

The error is thrown because find_element_by_class_name function belongs to webdriver.driver instead of webdriver. Note that this code will open two browser windows, not one, because:

"Web automation library for python which is based on the selenium framework"

If you look at the source you can see:

self.driver = webdriver.Chrome(executable_path=driverpath , chrome_options=options)

It means that you open two tabs in these two lines:

driver = webdriver.Chrome(executable_path= r'C:/Users/ryans/OneDrive/Desktop/Briefcase/Python Scripts/chromedriver.exe')

web = Browser()

You don't actually need that. May be fixed in this way:

import requests
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from webbot import Browser

web = Browser()
url = 'https://web_site'
web.go_to(url)

# 1st validation
web.type('email' , into='username')
web.click('Continue')

# 2nd validation
web.type('email' , into='username')
web.click('Next')

# password
web.type('pass' , into='password')
web.click('Next')

# Now you are logged in!!
url = 'https://web_page'
web.go_to(url)

# upload CSV Source File:
WebElement = web.driver.find_element_by_class_name('ignore-inline-attach')
web.driver.sendKeys("C:/my_path/test.csv");

# Close browser.
web.close_current_tag()

Glad to help you :)

Upvotes: 2

Related Questions