简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM