Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

python - Setting selenium to use custom profile, but it keeps opening with default

I am trying to use python and selenium to automate some tasks in firefox. When I download a file, that pop up comes up asking if you want to open or save, and a check box for do this every time with this kind of file. I have found that check box does not work unless you install the add on Web Page Fixer. I have that installed normally, but when I use python + selenium it uses a profile with no add ons.

The internet has instructed me to create another profile by closing Firefox, opening /Applications/Utilities, then typing the command:

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p

I then create a new profile that I will use with selenium. I set the name and change the folder name. The profile name is "PTI_Auto_Profile". The folder path displays as follows:

/users/User/Library/Application Support/Firefox/Profiles/Selenium/

When I am done. I click 'Start Firefox', and the following error appears on my terminal screen.

2013-04-11 11:57:30.422 firefox-bin[2248:707] invalid drawable
conf-room:~ User$ 2013-04-11 11:58:00.350 firefox-bin[2251:303] invalid drawable

I've tried the following to no success.

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(firefox_profile=profile) 

No error, default user.

profile = webdriver.FirefoxProfile(os.path.expanduser("~/Library/Application Support/Firefox/Profiles/Selenium/"))
driver = webdriver.Firefox(profile) 

No error, default user.

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv/xls")

driver = webdriver.Firefox(firefox_profile=fp)

Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined

Any ideas on what I am doing wrong? Thank you!

p.s. I am using mac os x 10.8.2, python 2.7, firefox 20

SOLUTION PROVIDED BY Corey Goldberg. This should work for all excel versions.

import os
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))
driver = webdriver.Firefox(profile)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined

getcwd() is not defined. So I assume you want the getcwd from the os module:

add: import os , and then invoke with os.getcwd().

or you could just add the import for this function: from os import getcwd

your example with the proper imports included:

import os
from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls')
driver = webdriver.Firefox(profile)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...