agurobuco
agurobuco

Reputation: 13

How to click on an image link using selenium resulting in TimeoutException error

I'm trying to click on the first image that appears for a given search in Flickr using Selenium as the code shows:

image = WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@style,'transform: translate(0px, 0px)')]//a[@role='heading']")))
image.click()

I've tried doing it step by step in python's IDLE and it works flawlessly but when i run the script it raises a TimeoutException as if the webelement never gets clickable. Any idea of what I am doing wrong ?

Upvotes: 1

Views: 87

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

As per the HTML DOM within the url to click on the first image that appears for a given search in Flickr using Selenium instead of using the style attribute I would suggest to use a more reliable attribute as follows:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.view.photo-list-view>div.view.photo-list-photo-view.awake a.overlay[href*='photos']"))).click()
    
  • Using XPATH:

    WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='view photo-list-view']/div[@class='view photo-list-photo-view awake']//a[@class='overlay' and @role='heading'][contains(@href, 'photos')]"))).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
    

Upvotes: 1

ewwink
ewwink

Reputation: 19154

its not clickable because the element is overlapped by other and need to hover to make the <a> element to be clickable, simple solution is to change condition to .presence_of_element_located()

Upvotes: 1

Related Questions