2016-11-03 88 views
1

我試圖在執行flickr.com搜索(https://www.flickr.com/search/?text=lake)後導航到「人員」部分。Selenium找不到特定元素

我無法找到<div id="content">(HTML圖像頂部的第6行)內的元素。基本上,無論我嘗試什麼,都會拋出一個NoSuchElementException

#! python3 

# Saves photos to file from flickr.com using specified search term 

import logging 
import os 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait 
import sys 

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

def flickr_images(): 
    try: 
     search_term, number_images = sys.argv[1:] 
    except: 
     print("Something went wrong. Command line input must be of \ 
    format: 'filename searchterm numbermessages'") 
     return 

    driver = webdriver.Firefox() 
    driver.get("https://www.flickr.com/") 

    # wait for page to load 
    WebDriverWait(driver, 10).until(EC.presence_of_element_located(\ 
     (By.ID, "search-field"))) 

    # find search text field and input search term 
    driver.find_element_by_id("search-field").send_keys(search_term) 

    # find and click search button 
    driver.find_element_by_class_name("search-icon-button").click() 

    WebDriverWait(driver, 5) 

    content = driver.find_element_by_id("content") 
    content.find_element_by_class_name("search-subnav-content") 

if __name__ == "__main__": 
    flickr_images() 

enter image description here

+0

一個問題: 它不應該是「driver.find_element_by_class_name (「search-subnav-content」)'而不是'content.find_element_by_class_name(「search-subnav-content」)''? –

+0

你期望'WebDriverWait(driver,5)'做什麼? – Andersson

+1

錯誤的成員和子成員。如果不抓住main_class,則無法處理子類。 – dsgdfg

回答

2

我會直接導航到搜索結果頁面和等待結果出現

driver.get("https://www.flickr.com/search/?text=" + search_term) 

# wait for the search results to appear 
wait = WebDriverWait(driver, 10) 
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-photos-results"))) 

content = driver.find_element_by_id("content") 
content.find_element_by_class_name("search-subnav-content")