Unable to locate an element with the xpath expression using Selenium - python

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

Related

Click and download excel file with Selenium with 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()

How to locate the LOAD MORE button using Selenium Python

I'm having trouble trying to figure out how to locate the path for a "Load More Button" on this site I am trying to scrape.
https://www.ufc.com/athletes/all
The button html:
<a class="button" href="?gender=All&search=&page=1" title="Load more items"
rel="next">Load More</a>
"Load More" == $0
::after == 0$
To click on the element LOAD MORE you need 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://www.ufc.com/athletes/all')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Load more items']"))).click()
Using XPATH:
driver.get('https://www.ufc.com/athletes/all')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Load more items']"))).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
You didn't provide any attempts and I can see 3 right off the top of my head that should work...
CSS selector, a[title='Load more items']
XPath, //a[text()='Load More']
By link text, "Load More"
In general you should prefer CSS selectors because they are more widely supported, faster, and have simpler syntax.
this is the XPATH for that button that you want
you can right-click on an element to inspect and right-click on it to copy XPath
driver.find_element(By.XPATH, ' //*[#id="block-
mainpagecontent"]/div/div/div[2]/div/div/ul/li/a').click()

Python, Selenium: how to click on javascript:void(0) element

I have an "onclick" element in a webpage,
<a href="javascript:void(0)" onclick=
"toIndexHtml('3','http://xxxxxxxxxxx/trade','0')">
<i></i>
<span></span>
trade
</a>
It is shown in the webpage as a button and I want to click on it, I tried to search for it using the following code:
driver.find_element_by_xpath("//a[contains(#onclick,'toIndexHtml')]").click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"/html/body/div/ul/li[3]/a"))).click()
Both are not working, please suggest if there is any other ways! Thanks in advance!
P.S.: I am using Chrome WebDriver and Chrome v64.
Your first locator looks perfect and should have worked.
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 CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='toIndexHtml']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick,'toIndexHtml')]"))).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

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