简体   繁体   English

Selenium TimeoutException 处理不起作用

[英]Selenium TimeoutException handling doesn't work

I am doing a project mentioned in the book 'Automate the Boring Stuff with Python'.我正在做“用 Python 自动化无聊的东西”一书中提到的一个项目。 Task is to download all images for a specific search tag from either flicks or imgur.任务是从 flicks 或 imgur 下载特定搜索标签的所有图像。 So I chose flickr but my Timeout Exception doesn't work even if it's defined already.所以我选择了 flickr,但我的超时异常即使已经定义也不起作用。

#! python3
# download all images from Flicker using user defined Tag
import logging, os, time, requests
import pyinputplus as pyip
from selenium import webdriver
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.common.exceptions import TimeoutException

logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')

# Ask user for a Tag
print("Hello user, this utility helps you to download images of specific 'tag' to your PC.\n")
tag = pyip.inputStr("Please choose a tag:", allowRegexes=[r'\s'])
os.makedirs(tag, exist_ok=True) # creates  folder named as user define tag

# get url
baseurl = "https://www.flickr.com/"
search_prefix= "search/?text="
search_suffix= "&media=photos"
search_url = baseurl + search_prefix + tag + search_suffix
logging.debug(f'{search_url}')

# opens firefox and get list of image links
driver = webdriver.Firefox()
driver.get(search_url)
time.sleep(2)
item_links = []
for item in driver.find_elements_by_class_name('overlay'):
    item_links.append(item.get_attribute('href'))

print('Python script has found %s images.\n' % (len(item_links)))
count = pyip.inputInt('How many images would you like to download?') 

# getting all links for images
image_links = []
for i in range(0,count):        
    image_link = str(item_links[i]) + 'sizes/l/'
    logging.debug(f'{image_link}')
    driver.get(image_link)
    try:
        webelement = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#allsizes-photo > img:nth-child(1)')))
        image_links.append(webelement.get_attribute('src'))
    except TimeoutException as e:
        pass

# downloading all images   
for link in image_links:
    response = requests.get(link)
    response.raise_for_status()
    file = open(os.path.join(tag,os.path.basename(link)), 'wb')
    for chunk in response.iter_content(100000):
        file.write(chunk)
    file.close()

print('Done.')

Everything is working fine until webelement is not located due to download restrictions even though "except TimeoutExceptions' is defined. I found out that the css_selector changes from #allsizes-photo > img:nth-child( 1 ) (if present) to #allsizes-photo > img:nth-child( 2 ) (if not) but I have an impression that the exception should be able to handle that. Could you please advise what am I doing wrong?一切正常,直到由于下载限制而找不到 webelement,即使定义了“除了 TimeoutExceptions”。我发现 css_selector 从 #allsizes-photo > img:nth-child( 1 ) (如果存在)更改为 #allsizes -photo > img:nth-child( 2 ) (如果没有)但我的印象是异常应该能够处理。你能告诉我我做错了什么吗?

Link examples as requested:根据要求链接示例:

webelement present flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l webelement 目前flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l

timeoutexception flickr.com/photos/ebanatawka/5062862631/sizes/l超时异常flickr.com/photos/ebanatawka/5062862631/sizes/l

Exception例外

webelement present存在的网络元素

I am using this CSS我正在使用这个CSS

div#allsizes-photo img

with visibility_of_element_located in explicit wait显式等待中具有visibility_of_element_located

Code:代码:

driver.get("https://www.flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l/")
webelement = WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"div#allsizes-photo img")))
print(webelement.get_attribute('src'))

Output Output

https://live.staticflickr.com/65535/48718018992_7cbf09802a_b.jpg

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

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