2016-11-19 87 views
0

我試圖抓取一個網頁,我需要通過多次單擊展開按鈕來展開項目列表。顯式等待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