0

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.waitthat 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']
Alisa
  • 1
  • 4
  • Did you try clicking just by finding the element by id='download'? Something like `driver.find_element_by_id('download').click()` .If nothing works, you can always use JavaScript to click on the 'download' link – shank087 May 10 '18 at 11:59
  • I tried the `driver.find_element_by_id('download').click()` But it doesn't works and there is no output too program runs without error. How can I use Javascript to click on download link ? I am not aware of that – Alisa May 10 '18 at 12:20
  • If you are using IE, then press F12 and move to the console window and type `document.getElementById('download').click();` . Check if it clicks on the link. – shank087 May 10 '18 at 12:27
  • Yes it does click – Alisa May 10 '18 at 12:31
  • Then just try this. `driver.execute_script("document.getElementById('download').click();");` . I am not sure about the syntax for Python. I guess it should work. Just correct the `execute_script` syntax if it doesn't work. – shank087 May 10 '18 at 12:33
  • Super... It just worked but the download just appears as a popup again I need to click on that to download it ? but generally it should be aitosaved – Alisa May 10 '18 at 12:40
  • Is the popup appearing on the bottom of the page? I believe you are using IE 11 – shank087 May 10 '18 at 12:44
  • yes its IE11 that I am using – Alisa May 10 '18 at 12:48
  • In IE11 - `ALT+N` command will shift the focus to the alert pop up at the bottom of the page. Using Java client libraries, I can just use the `Robot` Class to click on the `Save` button but I am not sure about Python. May be you need to google or wait for others to provide a solution for handling download popup. Try it out with `gtk.gdk.Display` class. Check this link - https://stackoverflow.com/questions/860013/is-there-a-python-equivalent-to-javas-awt-robot-class – shank087 May 10 '18 at 12:57

1 Answers1

0

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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Tried Both of them but still there is the same error. See edit for what I did – Alisa May 09 '18 at 16:44
  • @Alisa _TimeoutException_ is the outcome of **failed** _expected-conditions_. Debug your code through `find_element_by_*` inconjunction with `time.sleep()`. If you are able to locate the element and update the question with the observations. – undetected Selenium May 09 '18 at 16:45
  • Browser I am using is IE – Alisa May 09 '18 at 16:48
  • Its not able to locate via xpath or class but by id. – Alisa May 09 '18 at 17:49
  • @Alisa Try to use the xpath or the css I have provided with in my Answer to locate the element – undetected Selenium May 09 '18 at 17:58
  • It is not able to locate using xpath or css_selector that you provided. – Alisa May 10 '18 at 04:14
  • @Alisa Can you be specific with the exact _error_ you see with `find_element_by_*` inconjunction with `time.sleep()` ? – undetected Selenium May 10 '18 at 05:51
  • you can see when I find by "id" it prints element found but when I do the same by CSS selector or xpath it gives the following error pasted – Alisa May 10 '18 at 11:37
  • `((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']")))` should have been `("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']")` i.e. within _single first braces_ e.g. `()` – undetected Selenium May 10 '18 at 11:46
  • @Alisa As `find_element_by_*` padded with `time.sleep()` returned _NoSuchElementException_, _WebDriverWait()_ will always return _TimeoutException_. So first and foremost you have to address **NoSuchElementException** which wasn't evident earlier. Follow this discussion [Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome](https://stackoverflow.com/questions/47993443/selenium-selenium-common-exceptions-nosuchelementexception-when-using-chrome/47995294#47995294) to solve _NoSuchElementExeption_. – undetected Selenium May 10 '18 at 12:24
  • But I am not using chrome as browser, can we have a discussion over chat – Alisa May 10 '18 at 12:27
  • Hmmm, not now, may be after my office hours, an hour later from now – undetected Selenium May 10 '18 at 12:29