0

For automating downloading from a website, I am using this python script for automating click action. But the click does not work.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

browser = webdriver.Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link

I first got this error:

selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view

Then I add this line:

linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))

Now I get this error:

    linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

I tried the things that people used to solve this problem but did not work.

Peggy
  • 639
  • 9
  • 28
  • 2
    I just tried it with Chrome instead of Firefox and it seems to be working for me? It clicks the CCleaner link and goes to the Ccleaner page and just sits there. – G_M Mar 05 '18 at 17:22
  • Note, that you shouldn't use `implicitly_wait()` along with ExplicitWait – Andersson Mar 05 '18 at 17:25
  • The problem is with Firefox. Firefox wants an object to be scrolled into view before clicking it. Chrome, on the other hand, will scroll to the object to attempt the click. – sytech Mar 05 '18 at 19:36
  • @Andersson, if you know the element is outside your **implicit wait**, then why not use an **explicit wait**? In this case they are both set to `20`, so it does not really do anything, but still, why not use both? – PixelEinstein Mar 05 '18 at 19:56
  • @PixelEinstein , you can check [this](https://stackoverflow.com/questions/29474296/clarification-of-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc) to get some clarifications – Andersson Mar 05 '18 at 20:07
  • @Andersson, interesting. I have been using Explicit waits to override my Implicit waits for a very long time and have never noticed anything (probably because my implicit time is short relative to my explicit times). Even though the polling time is influencing my explicit time, I will continue to use it as I have, it is more effective in my case to use a short implicit and longer explicit when needed. Thanks. – PixelEinstein Mar 05 '18 at 20:16

1 Answers1

0

Your code seems to work with Chrome, I only added the bottom two lines and it downloaded CCleaner.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()  # Firefox()
browser.maximize_window()
browser.implicitly_wait(20)
browser.get('http://download.cnet.com/most-popular/windows/')
linkElem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "CCleaner")))
linkElem.click() # follows the "CCleaner" link

download = browser.find_element_by_id('download-now-btn-text')
download.click()
G_M
  • 3,342
  • 1
  • 9
  • 23
  • It still does not work. I tried all the things you people said. Have you tried this on Windows or Linux? Because I am running the script on Fedora. – Peggy Mar 06 '18 at 18:49
  • I got this error: selenium.common.exceptions.WebDriverException: Message: unknown error: missing or invalid 'entry.level' – Peggy Mar 06 '18 at 19:06
  • Yes, installing latest version of chromedriver solved that error. – Peggy Mar 06 '18 at 23:14
  • But last download click still does not work although the script is exited successfully: Process finished with exit code 0 – Peggy Mar 06 '18 at 23:15
  • 1
    Thanks for your help. I replaced find_element_by_id with the following: download = browser.find_element_by_partial_link_text('DOWNLOAD NOW') and it works! – Peggy Mar 07 '18 at 01:13
  • Hi. I faced some problems with automatic download from this website: http://downloads.zdnet.com/product/18512-10315544/ I use this command to click DOWNLOAD button but it cannot locate the element. download1 = browser.find_element_by_partial_link_text('Download') download1.click() – Peggy Mar 12 '18 at 03:26
  • yeah this question is solved. But you know the same script usually does not work with all the websites. I didn't ask another question because I don't think it is really different. – Peggy Mar 12 '18 at 03:30
  • yeah I know I've customised it for other four websites but for this website I couldn't yet figure it out. – Peggy Mar 12 '18 at 03:32
  • 1
    Thanks :) I learned to use this CSS selector also for submit button while login is needed. – Peggy Mar 12 '18 at 19:17