2016-08-22 2 views
0

나는 find 요소를 사용하려고합니다. http://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name; 그러나, 그들은 단지 절반의 시간과 일반적으로 더 간단한 사이트에서 작동하는 것 같습니다. 왜 그런지 궁금합니다.python 셀렌에서 원소를 찾는 데 어려움이 있습니다.

<a class="username" title="bruceleenation" href="/profile/u/3618527996"></a> 

사용 :

content = driver.find_element_by_class_name('username') 

을하지만 난 아무것도 점점없는거야 예를 들어, 현재 내가 찾는 데 노력하고 있어요. html로 어떤 작업을 수행하는에

https://gyazo.com/b2a0d389da26bbd325baaa5f915d0569 또는

<body> 

    <nav id="nav-sidebar" class="nav-main"></nav> 
    <main id="page-content" class="" style="margin-right: 17px; margin-bottom: 0px;"> 
    <header class="header-logged"></header> 
    <section class="page-content-wrapper"></section> 
    <section class="media-slider" style="display: block;"> 
     <div class="close-slider"></div> 
     <section id="slider" class="open" style="display: inline-block;"> 
      <a class="go-back" data-media-id="1322612612609855850_3618527996" title="Back to all media" href="javascript:void(0);"></a> 
      <section class="media-viewer-wrapper viewer" data-count-comments="0" data-count-likes="1" data-url-delete="/aj/d" data-url-comment="/aj/c" data-url-unlike="/aj/ul" data-url-like="/aj/l" data-user-id="3618527996" data-media-id="1322612612609855850_3618527996"> 
       <section class="mobile-user-info"></section> 
       <section class="desktop-wrapper"> 
        <section class="user-image-wrapper"> 
         <div class="image-like-click"></div> 
         <a class="user-image-shadow" href="javascript:void(0);"> 
          <img class="user-image" alt="" src="https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.0…493235_n.jpg?ig_cache_key=MTMyMjYxMjYxMjYwOTg1NTg1MA%3D%3D.2"></img> 
         </a> 
         <section class="image-actions-wrapper dropdown-anchor"></section> 
        </section> 
        <section class="media-viewer-info ui-front"> 
         <section class="user-info-wrapper text-translate-parent-wrapper "> 
          <a class="user-avatar-wrapper profile" title="bruceleenation" href="/profile/u/3618527996"></a> 
          <section class="user-info"> 
           <a class="username" title="bruceleenation" href="/profile/u/3618527996"> 

            bruceleenation 

           </a> 
           <p class="full-name"></p> 
           <div class="media-date-geo"> 
            <span></span> 
           </div> 
          </section> 

어떤 제안에서입니까? 나는 Xpath도 시도했다. ["//a[@class='username'"]

+0

는 당신에게 요소는 당신이 찾고있는 시점에서 존재 확실 해요? 페이지가 즉시 내용을 변경하는 것은 일반적입니다. –

+0

또한 time.sleep (N)을 수행해 보셨습니까? 때때로 페이지가 복잡하면로드하는 데 시간이 걸릴 수 있으므로 일부 구성 요소는 페이지에 즉시 나타나지 않습니다. –

답변

0

당신은 요소는 다음과 같이 제시 될 때까지 기다려야 WebDriverWait를 사용하여 시도해야합니다 : -

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 

content = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.username[title = 'bruceleenation']")))