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)
Related
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'm trying to run Selenium in AWS Lambda (Python) and recently learned that since Chromedriver version 2.31 they changed chromeOptions to goog:chromeOptions
https://chromedriver.storage.googleapis.com/2.31/notes.txt
https://www.selenium.dev/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html
I am running Chromium 86 with Selenium 3.14. I've been troubleshooting multiple error messages for the past hours and I suspect this is my underlying issue I just dont know how can I pass this argument to the driver or selenium. I've been trying the following with no luck
driver = webdriver.Chrome(options='goog:chromeOptions')
or
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('goog:chromeOptions')
EDIT
Here's a snippet of the code I'm using
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')
Any help is greatly appreciated
You saw it right.
You have to add the following import:
from selenium.webdriver.chrome.options import Options
Now you can add any of the arguments. As an example:
options = Options()
options.add_argument("start-maximized")
And finally:
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com/")
As an alternative,
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com/")
Update
ChromeOptions object: Most Chrome-specific capabilities are exposed through the ChromeOptions object. In some languages, this is implemented by the ChromeOptions class. In other languages, they are stored under the goog:chromeOptions dictionary in desired capabilities.
As an example, in Ruby:
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"goog:chromeOptions" => {"args" => [ "window-size=1000,800" ]})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
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 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