简体   繁体   English

如何使用硒单击图像链接导致TimeoutException错误

[英]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: 我试图单击使用Selenium的Flickr中给定搜索显示的第一张图片,代码显示如下:

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. 我已经尝试在python的IDLE中逐步进行操作,并且它可以完美运行,但是当我运行脚本时,它会引发TimeoutException ,好像webelement永远不会被点击。 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: 根据URL中HTML DOM ,单击使用Selenium而不是使用style属性在Flickr中为给定搜索显示的第一张图像,我建议使用更可靠的属性,如下所示:

  • Using CSS_SELECTOR : 使用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 : 使用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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM