2016-11-03 6 views
1

flickr.com 검색 (https://www.flickr.com/search/?text=lake)을 수행 한 후 '인물'섹션으로 이동하려고합니다.셀레늄이 특정 요소를 찾지 못했습니다.

나는 <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을 ("검색-subnav 컨텐츠")을 content.find_element_by_class_name '대신에'("검색-subnav-내용을") '? –

+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") 
관련 문제