Click and download excel file with Selenium with Python - python

Tried to download with Selenium and Python , the excel file by clicking the button on page but failed. Following is the class extracted from page:
<div class="tabPanel_func ts2_icon_map ts2_icon_excel" onclick="ExportExcel('tblTS2', tsData);"\>\</div\>`
I also want to download the file and then save to the specific drive location.
Following is my code :
url = "http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1"
driver = webdriver.Chrome()
driver.get(url)
Code trials:
driver.find_element(By.XPATH, "//\[contains(#onclick, ExportExcel('tblTS2', tsData)')\]")`
or
driver.find_element(By.XPATH, "//\*\[#class= 'tabPanel_func ts2_icon_map ts2_icon_excel'\]")`
or
driver.find_element(By.CSS_SELECTOR, "//button\[contains(#onclick,'ExportExcel('tblTS2', tsData)')\]").click()`
All approaches have failed.

The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CLASS_NAME:
driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "ts2_icon_excel"))).click()
Using CSS_SELECTOR:
driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.ts2_icon_excel[onclick^='ExportExcel']"))).click()
Using XPATH:
driver.get('http://www.aastocks.com/tc/stocks/market/high-low-stocks.aspx?catg=1&period=3&t=1')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#onclick, 'ExportExcel')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser snapshot:

Try the below XPath expression:
//*[contains(#class,'tabPanel_func ts2_icon_map ts2_icon_excel')]
XPath explanation: Searches the entire DOM(//*) which contains attribute = class and its value = tabPanel_func ts2_icon_map ts2_icon_excel
Above XPath expression will locate the below element:
Below is the full line of code to click the Export button:
driver.find_element(By.XPATH,"//*[contains(#class,'tabPanel_func ts2_icon_map ts2_icon_excel')]").click()

Try following xpath to click on export excel.
//div[contains(#onclick,'ExportExcel')]
code:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[contains(#onclick,'ExportExcel')]
"))).click()

Related

How to click a javascript button via Selenium in python

I am trying to pull some quotes on a website. To get the quote you have to click the Javascript button below:
Below is my python code to click the button:
driver = webdriver.Edge()
driver.get("http://generatorland.com/glgenerator.aspx?id=86")
driver.find_element(By.XPATH,"//a[#href='JavaScript:void(0);']").click()
However, when I run the code to test the button press, it throws an error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[#href='JavaScript:void(0);']"}
(Session info: MicrosoftEdge=110.0.1587.46)
Why can't it find the button, even when I specify the href sample for the XPATH to find?
You can move a step deeper and use the <img> tag.
Solution
The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a > img[alt='Confucius']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a/img[#alt='Confucius']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Unable to locate an element with the xpath expression using Selenium

I am now convinced that there is no solution to this. But in case there is someone out there who can help: Whole Element that I want to click on genesys cloud is (Its a part of a table):
<a target="_blank" data-bind="
attr: {
href: lastReportRun().reportUrl
},
lAttr: {
title: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
},
lString: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
" href="https://apps.usw2.pure.cloud/platform/api/v2/downloads/9161911a0307202a" title="XLSX">XLSX</a>
Element snapshot:
I am trying to locate this element to click on it and start downloading the .xlsx file. These are the things I have tried so far, but no luck. Can someone please help me understand where I can correct it.
Code trials:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
driver.find_element_by_link_text("XLSX").click()
driver.find_element_by_xpath(u'//a[text()="XLSX"]').click()
driver.find_element_by_xpath('//a[normalize-space(text())="XLSX').click()
driver.find_element(By.XPATH, "//input[#name='XLSX' and #value='XLSX']").click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//*input[#name='XLSX' and #value='XLSX']"))).click()
driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div[5]/a').click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*span[contains(., 'XLSX')]"))).click()
l=driver.find_element_by_xpath("//*a[#title='XLSX']")
l.click()
The desired element is a dynamic <a> element.
So to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://apps.usw2'][title='XLSX'][data-bind]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#href, 'https://apps.usw2') and text()='XLSX'][#title='XLSX' and #data-bind]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium Checkbox Selection

I'm trying to use selenium to click on a checkbox, as is depicted in the HTML of the picture. The think is, the checkbox input tag is the same for all four options, and they are distinguished only through the id="country_england" ... . I'm not sure how I can write code to select the england box and click it. Any help appreciated.
HTML Code:
use xpath:
//b[#id="country_england"]/../input
This finds the p tag that has that ID and then goes to the parent element and navigates back to input element
you can also use:
//b[#id="country_england"]/preceding-sibling::input
This finds the preceding element of p tag that has tag input
Code would be :
webD.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[#value="1861"]'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//b[#id = "country_england"]/preceding-sibling::input'))).click()
The desired element is a Angular element. So to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH and preceding:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding::input[1]"))).click()
Using XPATH and preceding-sibling:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding-sibling::input[1]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Update

How to click on the "Mail" button on the yahoo page using Selenium and Python

I got a BUTTON variable for those buttons:
GoMailsBTN = browser.find_element_by_class_name("D(ib) Fz(14px) Fw(b) Lh(24px) Pstart(38px)")
GoMailsBTN.click()
and there is not like any id you can go check it out by yourself if you want!
This is the error that pops out
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
Anyone got any idea why this is?
Snapshot of the button:
The class name is dynamic and can change, in this case, you can use xpath:
browser.find_element_by_xpath("//li//a[contains(#href ,'mail') and not(#id)]")
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://in.yahoo.com/?p=us')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#header-mail-button span"))).click()
Using XPATH:
driver.get('https://in.yahoo.com/?p=us')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='header-mail-button']//span"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:

How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python

I'm trying to click a link in a dropdown menu in Selenium.
I'm accessing the element like so:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
The link's href is javascript.void(0), and contains an onclick attribute which contains:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
I've tried triggering this with:
click()
and
driver.execute_script(link.get_attribute('onclick'))
Also an ActionChain mousing over the link and clicking it.
But none seem to work. How do I trigger this?
The element is a AJAX element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories