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
Related
I'm trying to click on a button that is generated using this javascript code
<script type='text/javascript' value name ="clickme"
src='//examplecode.com/example.js>
I'm using driver.findelement and it says "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable", heres my python code:
from webdriver_manager.chrome import ChromeDriverManager
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
import time
options = webdriver.ChromeOptions() # Initializing Chrome Options from the Webdriver
options.add_experimental_option("useAutomationExtension", False) # Adding Argument to Not Use Automation Extension
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("excludeSwitches", ["enable-automation"]) # Excluding enable-automation Switch
#options.add_argument("disable-popup-blocking")
#options.add_argument("disable-notifications")
options.add_argument("--enable-infobars")
options.add_argument("--enable-extensions")
options.add_argument("disable-gpu")
options.add_argument("start-maximized")
prefs = {"profile.default_content_setting_values.notifications" : 1}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options,executable_path=ChromeDriverManager().install())
driver.get("http://examplesite.com/")
tabName = driver.find_element(By.NAME, "clickme")
tabName.click()
time.sleep(200)
driver.quit()
maybe have some element layer upper than click() element
try this code:
tabname=WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CLASS_NAME, 'balabala')))
driver.execute_script("arguments[0].click();", tabname)
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)
I am trying to use selenium to download an excel file from a website. I am not sure why the code isn't allowing me to download it. I get an exit code 0 so everything ran successfully but I am not seeing the file in my downloads.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def scrape_mclellan_website():
url = 'https://www.mcoscillator.com/market_breadth_data/'
s = Service(ChromeDriverManager().install())
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(service=s)
driver.get(url)
download_link = driver.find_element(by=By.XPATH, value='//*[#id="data_table"]/a[1]/img')
download_link.click()
scrape_mclellan_website()
How to fix?
Wait until the element you try to click is present and click the <a> not the <img>:
download_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="data_table"]/a[1]')))
download_link.click()
Set your preferences for donwload folder and take care window is opening in right size:
prefs = {'download.default_directory':'ENTER PATH TO DOWNLOAD FOLDER'}
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_experimental_option("prefs",prefs)
Example (selenium 4)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
service = Service(executable_path='ENTER YOUR PATH TO CHROMEDRIVER')
prefs = {'download.default_directory':'ENTER PATH TO DOWNLOAD FOLDER'}
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.mcoscillator.com/market_breadth_data/')
download_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[#id="data_table"]/a[1]')))
download_link.click()
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()
I need some help.
There is URL: https://www.inipec.gov.it/cerca-pec/-/pecs/companies.
I need to click checkbox Captcha:
My code is look like:
import os, urllib.request, requests, datetime, time, random, ssl, json, codecs, csv, urllib
from urllib.request import Request, urlopen
from urllib.request import urlretrieve
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
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.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
driver.switch_to_default_content()
element = driver.find_elements_by_css_selector('iframe')[1]
driver.switch_to_frame(element)
driver.find_elements_by_xpath('//*[#id="recaptcha-anchor"]/div[1]').click()
During the execution, there is an error:
driver.find_elements_by_xpath('//*[#id="recaptcha-anchor"]/div1').click()
AttributeError: 'list' object has no attribute 'click'
Please, help to fix it.
Solution update (11-Feb-2020)
Using the following set of binaries:
Selenium v3.141.0
ChromeDriver v80.0
Chrome Version 80.0
You can use the following updated block of code as a solution:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
Original solution
Within the URL https://www.inipec.gov.it/cerca-pec/-/pecs/companies to invoke click() on the reCAPTCHA checkbox you need to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
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
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', chrome_options=options)
driver.get("https://www.inipec.gov.it/cerca-pec/-/pecs/companies")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox']/div[#class='recaptcha-checkbox-checkmark']"))).click()
I resolved this, you can try this with your landing website url.
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import SessionNotCreatedException
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": download_dir}
options.add_experimental_option("prefs", prefs)
options.add_argument("--no-sandbox")
driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options = options)
driver.get("https://www.google.com/recaptcha/api2/demo")
driver.maximize_window()
price = driver.find_element_by_xpath("//div[#class='g-recaptcha']")
price_content = price.get_attribute('innerHTML')
start = str(price_content).find(";k=")+len(";k=")
end = str(price_content).find("&co")
driver.implicitly_wait(20)
driver.execute_script("document.getElementById('g-recaptcha-response').style.display = '';")
recaptcha_text_area = driver.find_element_by_id("g-recaptcha-response")
recaptcha_text_area.clear()
recaptcha_text_area.send_keys(price_content[start:end])
#.....................................................................................
button = driver.find_element_by_id("recaptcha-demo-submit")