"Failed - Download error" while download a file using Selenium Python - python

I was trying to download a file using selenium but getting "Failed - Download error".
I tried to disable the safe browsing but it didn't work.
I have attached the screenshot and code as well.
logs:
DevTools listening on ws://127.0.0.1:53738/devtools/browser/d75dfd5b-1e3e-45c5-8edd-adf77dd9adb1
[2572:2724:0717/104626.877:ERROR:device_event_log_impl.cc(208)] [10:46:26.877] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import csv
from selenium.webdriver.chrome.options import Options
link_list = [
"stewartwatson.co.uk",
"peterkins.com",
"gavin-bain.co.uk",
"martinco.com",
"tmmsolicitors.co.uk",
"corecitilets.co.uk",
"coxandco.co",
"dunechtestates.co.uk",
"bidwells.co.uk",
"kwad.co.uk",
]
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\\Users\\Awais\\projects\\selenium\\web_email_extractor\\csv",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False,
"safebrowsing.ebabled": "false"
})
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.webemailextractor.com")
try:
driver.find_element_by_xpath('//button[contains(text(),"Close")]').click()
except:
pass
for i in link_list[0:5]:
text_area = driver.find_element_by_xpath('//textarea[#placeholder="Enter domain/websites list"]')
text_area.send_keys(i)
text_area.send_keys(Keys.ENTER)
submit = driver.find_element_by_xpath('//input[#value="Extract Email"]').click()
try:
btn = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), ' Process Completed')]")))
time.sleep(3)
csv_download = driver.find_element_by_xpath('//button[#class="dt-button buttons-csv buttons-html5"]').click()
except Exception as e:
print(e)

It worked when I removed the "r" from the path
"download.default_directory": r"C:\\Users\\Awais\\projects\\selenium\\web_email_extractor\\csv",
to:
"download.default_directory": "C:\\Users\\Awais\\projects\\selenium\\web_email_extractor\\csv",

Dear: When we configure the options of selenium is what will be defined in its configurations and as you write the route so considers it in the first photo you will see a well-configured route and a bad one, note bars there is the difference enter image description here
enter image description here

Related

Download a pdf from Chrome Viewer using selenium python

I am trying to log into an application, navigate to a pdf link, click on it. The pdf opens in chrome viewer. Download the pdf file. Download part is getting error: "DeprecationWarning: use options instead of chrome_options WebDriver = webdriver.Chrome(chrome_options=options)" and the pdf does not get downloaded
I've looked at all the solutions online and am doing something wrong. New to this.
Please correct me. Any help is much appreciated!
from time import sleep
import self as self
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import Chrome, ChromeOptions
*# mulitple drivers for other aspects of the code. Not relevant to the question so excluded.*
WebDriver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))
driver = WebDriver.get("open application") *(links changed)*
*#click on pdf file*
wait = WebDriverWait(WebDriver, 10)
WebDriver.find_element(By.ID, "XXXX").click() **(ID excluded as its restricted to post)**
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": "C:/Downloads/POD",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
)
WebDriver = webdriver.Chrome(chrome_options=options)
There are several issues here.
You've defined multiple Chromedriver instances. You should only need one.
WebDriver.get() doesn't return anything but you've assigned it to driver.
You've defined your ChromeOptions() after you've already created one driver instance.
DeprecationWarning is not an error, it's a warning. It's basically telling you that you're using code in a way that's not going to be supported in the near future.
Your code should look something like
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": "C:/Downloads/POD",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
)
WebDriver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()), options=options)
WebDriver.get("http://www.sample.com")
#click on pdf file
WebDriver.find_element(By.ID, "someId").click()

Excel File is not downloaded using selenium we driver. Element is clicked but file isn't getting downloaded

I tried to download one excel file using the python selenium web-driver. After running the code I noticed that file is not downloaded (may be a source restriction). I am unable to figure out whether this can be handled or not. below is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
ChromeDriver_Path = ChromeDriverManager().install()
options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')
Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
driver.get(Source_Url)
element_1 = driver.find_element_by_css_selector(
'#RbnzContent > div.table.summaryinfo-table > div:nth-child(5) > div.summaryinfo-data.col-lg-8.col-md-9.col-sm-12.col-xs-12 > a:nth-child(2)'
).click()
but when I check my directory I see that there are no files downloaded. I even checked the selenium browser window and I noticed that website isn't letting python script to download the file using automated way. Is there any way to fix this issue!!.
This is the url:
try adding a delay / wait before clicking the element.
Expected conditions explicit wait is the preferred way to do that.
Also, you could improve the locator.
Also in case the code you shared is all your code you can add a delay after the clicking on the download button otherwise the session will close immediately after the click so file will be not downloaded.
Let me know if this worked better:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
ChromeDriver_Path = ChromeDriverManager().install()
options = Options()
options.binary_location = GoogleChrome_Path
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument('window-size=1920x1080')
Source_Export_Path = r'myfolderpath'
options.add_experimental_option("prefs", {"download.default_directory": Source_Export_Path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
Source_Url = 'https://www.rbnz.govt.nz/statistics/j10-insurance-income-statement?__cf_chl_jschl_tk__=GXpMiuooASSLEEV4GrC0ODNHt3tzhq3PE2pXI_hoUsw-1641891789-0-gaNycGzNC6U'
driver = webdriver.Chrome(executable_path= ChromeDriver_Path, options = options)
wait = WebDriverWait(driver, 20)
driver.get(Source_Url)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(#href,'Statistics') and(contains(text(),'Insurance'))]"))).click()
time.sleep(10)

Still no changes in google colab : selenium in python

import time
import sys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver=webdriver.Chrome('chromedriver',options=options)
driver.get("https://rentry.co/wftw8/edit")
driver.implicitly_wait(20)
#print (driver.page_source)
try:
# here I selected the **span** element that I talk above
span = driver.find_element_by_xpath('//*[#id="text"]/div/div[5]/div[1]/div/div/div/div[5]/pre/span')
# change the innerText thwough js
driver.execute_script('arguments[0].innerText="Hello boy"', span)
# just wait for the id_edit_code to be present
edit = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "id_edit_code")))
edit.send_keys("iRfiNq6M")
# and same as you had it
#driver.find_element_by_id("submitButton").send_keys(Keys.ENTER)
#driver.find_element_by_link_text("Save").click()
driver.find_element_by_id("submitButton").click()
except:
print("Oops!", sys.exc_info()[0], "occurred.")
finally:
driver.close()
print("done")
There is no exception but still updating of the text is not reflecting in the url?
Even though there is timer which is enough for the whole code to get processed.
Then also there is no updation.
Currently the text is Hello if u visit the URL but I want it to be Hello boy using selenium , which is done by the below code line:
span = driver.find_element_by_xpath('//*[#id="text"]/div/div[5]/div[1]/div/div/div/div[5]/pre/span')
# change the innerText thwough js
driver.execute_script('arguments[0].innerText="Hello boy"', span)
But no updation!!?

How to click on download icon on chrome browser using python selenium

I want to download files by clicking on Download icon on Chrome browser.
I tried several ways like Xpath and CSS but it doesn't worked. Please let me know if there is any solution on this using Python 3.x and selenium.
Below is code that I have tried,
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
class TEAutomation:
def automateTask(self):
chromeOptions = Options()
chromeOptions.add_experimental_option("prefs",{"download.default_directory": "/home/vishal/Documents/PythonProgram/"})
baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
driver.maximize_window()
driver.get(baseUrl)
driver.implicitly_wait(10)
driver.find_element(By.XPATH,'//*[#id="download"]').click()
#driver.find_element(By.CSS_SELECTOR, '#download').click()
time.sleep(5)
driver.quit()
molexAuto = TEAutomation()
molexAuto.automateTask()
Thank you in advance.
Maybe the element is still not loaded when you try to click it, try waiting for it with WebDriverWait, I don't have chrome so you will have to test this yourself:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
class TEAutomation:
def automateTask(self):
chromeOptions = Options()
prefs = {
"download.default_directory": "/home/vishal/Documents/PythonProgram/",
"plugins.always_open_pdf_externally": True
}
chromeOptions.add_experimental_option("prefs", prefs)
baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
driver.implicitly_wait(10)
driver.maximize_window()
driver.get(baseUrl)
time.sleep(5)
driver.quit()
molexAuto = TEAutomation()
molexAuto.automateTask()

Python - Can't download file using selenium chromewebdriver - 'Failed - Download error'

I was trying to download a file from google chrome using selenium. The code I used below was working fine. But somehow it didn't work anymore. Any ideas?
import os.path
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)
driver.get(RAWDATA_URL)
time.sleep(5)
the xpath below is just copying from the HTML so should be correct
driver.find_element_by_xpath("//*[#id='main']/table[38]/tbody/tr[2]/td[5]/a").click()
I also tried the get method:
driver.get("https://oui.doleta.gov/unemploy/csv/ar9047.csv")
I was expecting the csv file could download successfully. But google chrome just tell me that "Fail- Download error'.
UPDATE: The question above is simplified by me. There are actually two steps in my project. First downloading the data from one site and then navigating to another to download the csv data.
import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
SUMMARY_URL = "https://oui.doleta.gov/unemploy/reemploy.asp"
RAWDATA_URL = 'https://oui.doleta.gov/unemploy/DataDownloads.asp'
REEMPLOYMENT_QTR = '09/30/2018'
options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : SAVE_PATH, "download.prompt_for_download": False}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path = DRIVE_PATH, chrome_options = options)
First Step:
driver.get(SUMMARY_URL)
time.sleep(5)
select = Select(driver.find_element_by_id('qtr'))
select.select_by_value(REEMPLOYMENT_QTR)
driver.find_element_by_xpath("//input[#name='submit'][#type='submit']").click()
re_table = driver.find_element_by_xpath("//*[#id='content']/table")
state = []
value = []
for re in re_table.find_elements_by_tag_name('tr'):
c = 0
for ele in re.find_elements_by_tag_name('td'):
if c == 0:
state.append(ele.text.encode('utf8'))
c += 1
else:
value.append(ele.text.encode('utf8'))
reemployment = pd.DataFrame({'state' : state, AS_OF_DATE : value})
reemployment = reemployment[['state', AS_OF_DATE]]
Second Step(my original question):
driver.execute_script("window.open('');")
time.sleep(5)
driver.switch_to.window(driver.window_handles[1])
time.sleep(5)
driver.get(RAWDATA_URL)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[#title='Data']"))).click()
my problem is my save path for default directory has issue:
it was 'C:/Users/...' but should have been 'C:\Users\...' like below
chrome_options = webdriver.ChromeOptions()
prefs = {
'download.default_directory': 'C:\\Users\\<username>\\Documents\\test\\',
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
}
chrome_options.add_experimental_option('prefs', prefs)
Presumably you are trying to invoke click() on the element with text as Data from the ETA 9047 section and to achieve that you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://oui.doleta.gov/unemploy/DataDownloads.asp")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[text()='ETA 9047']//following::table[1]//tr/td/a[#title='Data']"))).click()
Browser Snapshot:
PS: Ensure that you are using Selenium v3.141.59 with ChromeDriver / Chrome v76.0

Categories