2017-02-07 1 views
0

このページ(http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I)のWebクローラーを作成して各ページの在庫リストを収集しました。まず最初に、私のコードは "Search Menu"の部分のドロップダウンメニューを操作することから始まりますが、反復中にページの読み込みと保持に問題があります。私がしたいのは、ページを読み込んで、ページのクロールアクションが完了するまでそのページを保持することです。Selenium Pythonを使用してページを読み込んだ後、特定のページを保持する方法はありますか?

以下

は私のコードです:

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.common.exceptions import WebDriverException 
from selenium.common.exceptions import StaleElementReferenceException 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import Select 
from selenium.common.exceptions import NoSuchElementException 
from urllib import parse 
from time import sleep 

self.link = 'http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I' 
self.driver = webdriver.PhantomJS() 
self.driver.set_window_size(1920, 1080) 
self.driver.get(self.link) 
self.wait = WebDriverWait(self.driver, 10) 

def option2_menu_loaded(inDriver): 
     path = '//select[@id="level2_no"]' 
     return inDriver.find_element_by_xpath(path) 

self.wait.until(option2_menu_loaded) 

while True: 
    try: 
     select_option2_values = [ 
      ('%s' % o.get_attribute('text'), '%s' % o.get_attribute('value')) 
      for o 
      in Select(self.driver.find_element_by_css_selector("#level2_no")).options 
      if o.get_attribute('text') != '세부등급'] 
    except (StaleElementReferenceException, NoSuchElementException): 
     print("=======Exception Found - Option2 Save=====") 
     self.driver.refresh() 
     self.driver.implicitly_wait(1.5) 
     continue 
    break 

for option2 in select_option2_values: 
    self.csv.setCarTitle(ma, mo, de, option1[0], option2[0]) 

    print(option2[0], option2[1]) 
    self.driver.implicitly_wait(0.5) 

    while True: 
     try: 
      Select(self.driver.find_element_by_css_selector("#level2_no")).select_by_value(option2[1]) 

     except (StaleElementReferenceException, NoSuchElementException): 
      print("=======Exception Found - Option2 Request=====") 
      self.driver.refresh() 
      self.driver.implicitly_wait(1.5) 
      self.driver.refresh() 
      continue 
     break 

私は5行目の後に "self.wait.until(EC〜。)" のコードのいくつかのタイプを推測、 "self.wait.until(option2_menu_loaded)" かもしれません助けて。私はたくさん試しましたが、解決策を見つけることができません。

私はこれを解決するために手伝ってください。

答えて

0
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"level2_no"))) 

は、要素を操作する前に要素が存在するのを待つ場合に必要な要素です。インポートコードの先頭に - from selenium.webdriver.common.by import By

参考までに、選択タグには1つのオプションしか含まれていないことが気付きました。従ってselect_option2_values配列は、#level2_noが今後より多くの要素を含むと予想しない限り、空になります。

<select id="level2_no" name="level2_no" onmousedown="$('.maker').hide();" onchange="car_depth_step_new(this.value, 4);" style="width:112px" title="세부등급 선택"> 
<option value="">세부등급</option> 
</select> 
関連する問題