2016-11-19 59 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