câu hỏi cổ nhưng,
xem xét như thế nào WedDriverWait
công trình, trong một ví dụ độc lập từ selen:
def is_even(n):
return n % 2 == 0
x = 10
WebDriverWait(x, 5).until(is_even)
chí này đợi tối đa 5 giây cho is_even(x)
trở True
bây giờ, WebDriverWait(7, 5).until(is_even)
sẽ mất 5 giây và họ nâng cao một TimeoutException
Hóa ra, bạn có thể trả lại bất kỳ giá trị không Falsy và nắm bắt nó:
def return_if_even(n):
if n % 2 == 0:
return n
else:
return False
x = 10
y = WebDriverWait(x, 5).until(return_if_even)
print(y) # >> 10
Bây giờ xem xét như thế nào phương pháp của EC
hoạt động:
print(By.CSS_SELECTOR) # first note this is only a string
>> 'css selector'
cond = EC.presence_of_element_located(('css selector', 'div.some_result'))
# this is only a function(*ish), and you can call it right away:
cond(driver)
# if element is in page, returns the element, raise an exception otherwise
Có thể bạn sẽ muốn thử một số thứ e:
def presence_of_any_element_located(parent, *selectors):
ecs = []
for selector in selectors:
ecs.append(
EC.presence_of_element_located(('css selector', selector))
)
# Execute the 'EC' functions agains 'parent'
ecs = [ec(parent) for ec in ecs]
return any(ecs)
này sẽ làm việc nếu EC.presence_of_element_located
trở False
khi selector
không tìm thấy trong parent
, nhưng nó đặt ra một ngoại lệ, một dễ hiểu workaround sẽ là:
def element_in_parent(parent, selector):
matches = parent.find_elements_by_css_selector(selector)
if len(matches) == 0:
return False
else:
return matches
def any_element_in_parent(parent, *selectors):
for sel in selectors:
matches = element_in_parent(parent, selector)
# if there is a match, return right away
if matches:
return matches
# If list was exhausted
return False
# let's try
any_element_in_parent(driver, 'div.some_result', 'div.no_result')
# if found in driver, will return matches, else, return False
# For convenience, let's make a version wich takes a tuple containing the arguments:
cond = lambda args: any_element_in_parent(*args)
cond((driver, 'div.some_result', 'div.no_result'))
# exactly same result as above
# At last, wait up until 5 seconds for it
WebDriverWait((driver, 'div.some_result', 'div.no_result'), 5).until(cond)
Mục tiêu của tôi là để giải thích, artfulrobot đã đưa ra một đoạn trích để sử dụng chung các phương pháp thực tế EC
, chỉ cần lưu ý rằng
class A(object):
def __init__(...): pass
def __call__(...): pass
Chỉ là một cách linh hoạt hơn để xác định các hàm (thực tế, 'giống như chức năng', nhưng điều đó không liên quan trong ngữ cảnh này)
hãy xem http://stackoverflow.com/questions/7781792/selenium- này waitforelement, có thể giúp bạn .. –
Lớp ExpectedConditions gọn gàng mọi thứ nhưng tôi đã học được rằng nó không phải là rất đáng tin cậy (ở dạng thuần túy) và bạn cần nắm bắt rõ ràng các điều kiện ngoại lệ liên quan đến việc sử dụng nó và đôi khi lặp lại thử lại trên thất bại. – djangofan