我正在测试一个网站,当该网站正在处理时,它会放置一个灰色加载div。问题是div 在网站加载时立即存在,您可以判断它正在运行的唯一方法是内联 CSS 样式发生变化。
基本上在运行时它是:
<div id="loading" class="loading hide" style="display: block;">text</div>
不运行时,它是:
<div id="loading" class="loading hide" style="display: none;">text</div>
我怎样才能让硒在点击链接后等待,直到内联样式发生变化?我正在使用python和chrome webdriver。
您可以创建自定义等待条件:
class element_has_style(object):
"""An expectation for checking that an element has a particular style.
locator - used to find the element
returns the WebElement once it has the particular style
"""
def __init__(self, locator, style):
self.locator = locator
self.style = style
def __call__(self, driver):
element = driver.find_element(*self.locator) # Finding the referenced element
if self.style in element.get_attribute("style"):
return element
else:
return False
# Wait until an element with id='loading' has style 'display: none;'
wait = WebDriverWait(driver, 10)
element = wait.until(element_has_style((By.ID, 'loading'), "display: none;"))
驱动程序将等到元素具有特定样式。 有关更多信息,请查看此链接