0

I want to download the image at this site https://imginn.com/p/CXVmwujLqbV/ via the button. but i always fail. this is the code i use.

driver.find_element_by_xpath('/html/body/div[2]/div[5]/a').click()

2 Answers2

0

Well, Check this post for downloading resource. Picture has 'src' attribute in 'img' tag, that holds it.

Also, (though it just might be simplification just for this question), do not hardcode your xpath. Learn to code nicely using "Page Object Pattern".

user2678074
  • 768
  • 3
  • 9
  • 22
0

There are several possible problems here:

  1. You need to add a delay / wait before accessing this element.
  2. You have to scroll the page since the element you wish to click is initially out of the view.
  3. You should improve you locator. It's highly NOT recommended to use absolute XPaths.
    This should work:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.set_window_size(1920,1080)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

driver.get("https://imginn.com/p/CXVmwujLqbV/")

button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.downloads a")))
time.sleep(0.5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
#actions.move_to_element(button).perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.downloads a"))).click()

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • still not working, I found: ElementClickInterceptedException: Message: ..... so maybe the scrape is the iframe from the ads. – fandyekananda Dec 13 '21 at 09:24
  • That's because of add element appearing on the bottom of that page. I have replaced the scroll to element by scroll entire page for the page height. Also set the window size. Let me know if it works now – Prophet Dec 13 '21 at 11:01