2016-11-19 4 views
0

私は展開ボタンを何回かクリックして項目リストを展開する必要があるWebページを削り取ろうとしています。Python上でセレンを明示的に待ちます

私はこれをスマートな方法で研究したので、予想通りの状態(element_to_be_clickable)で明示的な待機を使用しようとしています。ボタンがまだページで提供されて展開する場合、単にテスト

from selenium import webdriver 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import NoSuchElementException 
from bs4 import BeautifulSoup 
from selenium.webdriver.support.ui import WebDriverWait 
import time 

btn_xpath = '//*[@id="contents"]/div[1]/div[2]/div/div[1]' 

browser = webdriver.Chrome('/Users/dongpark/Downloads/chromedriver') # calling chrome driver from local folder 
browser.get('http://cu.bgfretail.com/event/plus.do?category=event&depth2=1&sf=N') 
wait = WebDriverWait(browser, 20) 
time.sleep(8) 


def check_exists_by_xpath(xpath): 

    try: 
     browser.find_element_by_xpath(xpath) 
    except NoSuchElementException: 
     return False 
    return True 


while True: 

    button = check_exists_by_xpath(btn_xpath) 

    if button is False: 
     print "done" 
     break 
    else: 
     print "more" 
     wait.until(EC.element_to_be_clickable((By.XPATH, btn_xpath))) 
     browser.find_element_by_xpath(btn_xpath).click() 

check_exists_by_xpath

は、ここに私のテストコードです。

私はこれを実行すると、私が手:

File "/Users/dongpark/Documents/kuk/firstSelenium/test.py", line 37, in <module>  browser.find_element_by_xpath(btn_xpath).click() 
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (418, 920). Other element would receive the click: <div class="ico"></div> 
    (Session info: chrome=54.0.2840.98) 
    (Driver info: chromedriver=2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1),platform=Mac OS X 10.12.0 x86_64) 

クリックする前に、私はちょうどそれが動作しますが、私はそれをより効率的にしたい十分な睡眠が得られた場合。

答えて

1

要素の存在を待つためにあなたのcheck_exists_by_xpathを変更します。

def check_exists_by_xpath(xpath): 

    try: 
     wait.until(EC.presence_of_element_located((By.XPATH, xpath)) 
    except NoSuchElementException: 
     return False 
return True 
関連する問題