简体   繁体   中英

Expected_Conditions returns TimeoutException while using element_to_be_clickable method through selenium & python 3

I am trying to automate using selenium and python 3 , below is my code what I have tried :

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
)
element.click()

HTML :

<a href="javascript:void(0);" 
onclick="if(!alen_Button_checkClick('download',event)){return true;};return 
htmlbSL(this,2,'download:downloadBtn')" 
onkeypress="if(!alen_Button_checkClick('download',event)){return 
true;};return htmlbSL(this,2,'download:downloadBtn')" class="urBtnStd1 urV" 
id="download" ct="Button" title="Download To Excel" style="white- 
space:nowrap;">
    <span class="urBtnPadding">
        Download To Excel
    </span>
</a>

I want to click on "Download To Excel button" but everytime I execute the code whether I search it by xpath or class it doesn't executes and gives timeout error when I increase timeout it doesn't helps or I use Driver.wait that also doesn't helps.

stacktrace :

EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
  selenium.common.exceptions.TimeoutException: Message

What am I missing here ?

EDIT 2 :

time.sleep(5)
if driver.find_element_by_id('download'):
    print ("Element exists")
time.sleep(5)
if driver.find_element_by_xpath((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))):
    print ("Element exists1")

Stacktrace of edit 2 :

Element exists
Traceback (most recent call last):
  File "C:\Users\c5242046\Desktop\test2\backupautomation\bkpalertv1\bkpv1.py", line 73, in <module>
    if driver.find_element_by_xpath((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))):
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == //a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']

To click on the element with text as Download To Excel you have to induce WebDriverWait and can use either of the following options :

  • CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.urBtnStd1.urV#download[title='Download To Excel']>span.urBtnPadding"))).click() 
  • XPATH :

     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))).click() 

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