标题建议需要等待动态加载的材料。每次滚动到页面末尾时都会显示该材料。
目前正尝试通过以下方式执行此操作:
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是问题吗(不是那么熟悉)?即使在使用硒滚动时显示样式确实发生变化,它也会保持超时。
答案 0 :(得分: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