2016-01-21 53 views
1

正如标题所示,需要等待动态加载的材质。每次滚动到页面末尾时,材质都会显示。使硒驱动程序等待,直到元素样式属性发生变化

目前试图通过以下要做到这一点:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located(
    (By.XPATH, "//*[@id='inf-loader']/span[1][contains(@style, 'display: inline-block')]"))) 

是XPath这里的问题(不是所有的熟悉了)?即使硒滚动时显示样式发生变化,它也会保持超时。

回答

4

我将等待display风格与自定义的预期状态更改为inline-block,并使用value_of_css_property()

from selenium.webdriver.support import expected_conditions as EC 

class wait_for_display(object): 
    def __init__(self, locator): 
     self.locator = locator 

    def __call__(self, driver): 
     try: 
      element = EC._find_element(driver, self.locator) 
      return element.value_of_css_property("display") == "inline-block" 
     except StaleElementReferenceException: 
      return False 

用法:

wait = WebDriverWait(driver, 5) 
wait.until(wait_for_display((By.XPATH, "//*[@id='inf-loader']/span[1]"))) 

您也可以尝试tweak the poll frequency到使其更频繁地检查条件:

wait = WebDriverWait(driver, 5, poll_frequency=0.1) # default frequency is 0.5