I'm pretty new in selenium and getting an error with ChromeWebDriver.
I'm using: Chrome 36, ChromeWebDriver 2.10, Windows 7
Here's my code:
from selenium import webdriver
webD = webdriver.Chrome();
But I get the response
unknown error : chrome field to start
How can I fix this?
You may need to download the chrome executable driver from http://chromedriver.storage.googleapis.com/index.html and set the executable path accordingly.
Sample Python Code :
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "./chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Firefox()
driver.get("http://www.python.org")
print driver.title
assert "Python" in driver.title
For more information and end to end script follow
Reference
Related
I am trying to automate the download of a file to the /tmp directory in AWS Lambda using Chromedriver from this website https://registry.verra.org/app/search/VCS/All%20Projects. The steps are to 1) click on the 'Search' button, 2) wait for the results to load, 3) click on the 'Excel' logo to download the file.
I've referenced and tried the code provided in these 2 questions to change the download directory of Chromedriver but the file is not downloaded in the /tmp path.
AWS Lambda download a file using Chromedriver
prefs = {
"profile.default_content_settings.popups": 0,
"download.default_directory": r"/tmp",
"directory_upgrade": True
}
options.add_experimental_option("prefs", prefs)
Unable to change the default download location of chrome in AWS lambda to /tmp using Selenium
options = webdriver.ChromeOptions()
prefs = {"browser.downloads.dir": "//tmp//", "download.default_directory": "//tmp//", "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
For reference, here is my code which works fine in local but not in AWS Lambda.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
import requests
import requests.auth
import json
import csv
def lambda_handler(event, context):
# change directory to /tmp folder
os.chdir('/tmp')
# get dataset from website
options = Options()
options.binary_location = '/opt/headless-chromium'
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--single-process')
options.add_argument('--disable-dev-shm-usage')
## SAVE TO TMP DIRECTORY
# set download settings
prefs = {
"profile.default_content_settings.popups": 0,
"download.default_directory": r"/tmp",
"directory_upgrade": True
}
options.add_experimental_option("prefs", prefs)
## open Chrome webdriver
driver = webdriver.Chrome('/opt/chromedriver',options=options)
driver.maximize_window()
driver.get('https://registry.verra.org/app/search/VCS/All%20Projects')
# wait for 60 seconds for website content to load
print("Waiting for website to load...")
element1 = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '/html/body/apx-root/div/div/apx-search-page/div/apx-search-container/div/div[2]/div/div[1]/apx-search-selection-criteria/div/form/div[2]/div/button[1]')))
print("Website loaded!")
# click on search button to load results
search_btn = driver.find_element(By.XPATH, '/html/body/apx-root/div/div/apx-search-page/div/apx-search-container/div/div[2]/div/div[1]/apx-search-selection-criteria/div/form/div[2]/div/button[1]')
search_btn.click()
# wait for results to load for 100 seconds - determine by checking the page numbers
element2 = WebDriverWait(driver, 100).until(EC.presence_of_element_located((By.XPATH, '/html/body/apx-root/div/div/apx-search-page/div/apx-search-container/div/div[2]/div/div[2]/apx-project-search-results/div/div/kendo-grid/kendo-pager/kendo-pager-numeric-buttons/ul/li[1]/a')))
print("Results loaded!")
# wait for download button to load for 100 seconds - determine by detecting presence of download button
element = WebDriverWait(driver, 100).until(EC.presence_of_element_located((By.XPATH, '/html/body/apx-root/div/div/apx-search-page/div/apx-search-container/div/div[2]/div/div[2]/apx-project-search-results/div/apx-search-results-header/div/button[1]')))
download_btn = driver.find_element(By.XPATH, '/html/body/apx-root/div/div/apx-search-page/div/apx-search-container/div/div[2]/div/div[2]/apx-project-search-results/div/apx-search-results-header/div/button[1]')
# click on download button
# if element is not clickable
filepath = driver.execute_script("arguments[0].click();", element)
# wait for 60 seconds for file to download
time.sleep(60)
# check if file is downloaded to /tmp directory
# Method 2
list = os.listdir('/tmp')
print("list", list)
response = {
"statusCode": 200,
"body": "Selenium Headless Chrome Initialized"
}
return response
Could this be a versioning issue? Because my code only works if the runtime settings is for Python 3.7.
Selenium version: selenium/python/lib/python3.7/site-packages selenium==3.8.0\ (runtime Python 3.7)
Chromedriver version: https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip\ (only runtime Python 3.9 works, changing to Python 3.7 does not work)
Headless Chrome: https://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-41/stable-headless-chromium-amazonlinux-2017-03.zip
I am trying to extra data by return error
NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"//*[#id=\'searchpopbox\']"}' ; Stacktrace:
at FirefoxDriver.findElementInternal_ (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/driver_component.js:8444)
at FirefoxDriver.findElement (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/driver_component.js:8453)
at DelayedCommand.executeInternal_/h (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10456)
at DelayedCommand.executeInternal_ (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10461)
at DelayedCommand.execute/< (file:///tmp/tmpjVcHQR/extensions/fxdriver#googlecode.com/components/command_processor.js:10401)
My code is as below and I am trying to get the list from the link
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
browser = webdriver.Firefox(profile)
url = 'https://www.bursamarketplace.com/index.php?tpl=th001_search_ajax'
browser.get(url)
time.sleep(15)
a = browser.find_element_by_xpath("//*[#id='searchpopbox']")
print a
I am seeking your help to get the right xpath for the url.
This gets all the listing for that table.
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.bursamarketplace.com/index.php?tpl=th001_search_ajax")
time.sleep(15)
a = driver.find_element_by_xpath("//*[#id='searchpopbox']")
print(a.text)
Or without chromedrivermanager same thing applies to firefox
.Chrome(executable_path='absolutepathofchromedriver.exe')
I am trying to download the file automatically from system file download dialog by setting preference in firefox profile in my python selenium code , but my code is not working.
Browser : Firefox 72.0
Selenium Version : 3.14
OS : linux Ubuntu
Filetype to download: *.enc (encrypted file type)
Path of firefox in linux : /usr/bin/firefox
Code :
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)
Hi #Sum i have resolving, my problem was a different Content-Type.
Use this example to resolve your problem , and to understand your Content-Type: https://stackoverflow.com/a/36356422/12911814
In my case the Content-Type was "application/force-download" not "application/pdf"
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.dir", "<path>")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")
These settings worked for me. Hope it might help you.
I have the same problem with Firefox 72.0, but with pdf files. This is the code:
fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
driver = webdriver.Firefox(firefox_profile=fp)
I have tried all possible preferences, but it always triggers the download alert.
Try this will work like charm ......
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import pyautogui
try :
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
# Click the OK button and close
time.sleep(5)
webelem = driver.find_element_by_id('download')
webelem.click()
time.sleep(5)
print('press enter')
pyautogui.press('enter')
except Exception as err:
print('ERROR: %sn' % str(err))
driver.quit()
The correct MIME type for .enc is "text/x-uuencoded"
Updated as below in code and it's working :
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")
while run the script fire fox is launched correctly,but it is not getting the URL,Even i upgrade all like selenium,pip,fire fox too..give me a solution.how to resolve this error.
Try this code:
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> driver.get("http://www.python.org")
You make sure you add http:// to your URL.
how to minimize a firefox window by using selenium and python
i tried with
try:
body=None
body = driver.find_element_by_tag_name("body")
body.send_keys("{%+" "+N}")
print "entered keys"
except NoSuchElementException:
print "item body is not exists"
code:2
------
body.send_keys(Keys.CONTROL+Keys.ESCAPE+'D')
code:3
------
body.send_keys("{%" "n}")
Nothing worked for me i want to minimize my firefox window while running or after invoking
or run in invisible mode which has no focus
The following code should help:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.google.com")
actionChain = ActionChains(driver).key_down(Keys.ALT)
actionChain.send_keys(Keys.SPACE)
actionChain.send_keys("n")
actionChain.perform()