简体   繁体   中英

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 ?

它不可点击,因为该元素与其他元素重叠并且需要hover以使<a>元素可点击,简单的解决方案是将条件更改为.presence_of_element_located()

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 

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