Selenium TimeoutException 处理不起作用
Selenium TimeoutException handling doesn't work
我正在做书上提到的一个项目'Automate the Boring Stuff with Python'。任务是从 flicks 或 imgur 下载特定搜索标签的所有图像。所以我选择了 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.')
一切正常,直到由于下载限制而找不到 webelement,即使定义了“除了 TimeoutExceptions”。我发现 css_selector 从 #allsizes-photo > img:nth- child(1)(如果存在)到#allsizes-photo > img:nth-child(2)(如果没有)但是我我的印象是异常应该能够处理。你能告诉我我做错了什么吗?
Link 要求的例子:
存在网络元素 flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l
超时异常 flickr.com/photos/ebanatawka/5062862631/sizes/l
Exception
webelement present
我正在使用这个CSS
div#allsizes-photo img
with visibility_of_element_located
in 显式等待
代码:
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'))
输出
https://live.staticflickr.com/65535/48718018992_7cbf09802a_b.jpg
我正在做书上提到的一个项目'Automate the Boring Stuff with Python'。任务是从 flicks 或 imgur 下载特定搜索标签的所有图像。所以我选择了 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.')
一切正常,直到由于下载限制而找不到 webelement,即使定义了“除了 TimeoutExceptions”。我发现 css_selector 从 #allsizes-photo > img:nth- child(1)(如果存在)到#allsizes-photo > img:nth-child(2)(如果没有)但是我我的印象是异常应该能够处理。你能告诉我我做错了什么吗?
Link 要求的例子:
存在网络元素 flickr.com/photos/bjarnekosmeijer/48718018992/sizes/l
超时异常 flickr.com/photos/ebanatawka/5062862631/sizes/l
Exception
webelement present
我正在使用这个CSS
div#allsizes-photo img
with visibility_of_element_located
in 显式等待
代码:
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'))
输出
https://live.staticflickr.com/65535/48718018992_7cbf09802a_b.jpg