我有一个网站,其中有一个按钮"显示更多",一个人可以点击喜欢的频率.在某种程度上,会有一个同名的<span>
,一旦我看到那个,我就想停止点击这个按钮.
遗憾的是,当单击该按钮时,需要一些时间才能将所有内容展开并再次显示该按钮.在该时刻期间,标签"aria-busy="true"
被设置.
因此,目前,我的代码可以工作,但我会等待10秒,直到再次按下按钮.这太长了,但当然,如果我将其设置得太低,操作可能会失败.
我当前的代码是:
# Params: url, button-class, span_label
waiting_time = 10
options = Options()
driver = webdriver.Chrome(options=options)
driver.get(url)
wait = WebDriverWait(driver, waiting_time)
button = wait.until(
EC.element_to_be_clickable((By.CLASS_NAME, button-class))
)
while True:
try:
button.click()
wait.until(EC.visibility_of_element_located((By.XPATH, span_label)))
logging.debug("Found last block, continue")
break # Exit the loop if the span element is found
except TimeoutException:
continue # Continue clicking the button if the span element is not found within the timeout
content = driver.page_source
driver.quit()
我已经用过WebDriverWait了,但那个不做隐式等待.此外,我有一种感觉,这种循环方法相当糟糕.
所以我的问题是:有没有更高效、更好的代码来点击按钮,直到跨度出现?