把wd.find_element(By.CSS_SELECTOR,‘.frozen-table’)放在while循环中,如果不存在就循环等待,如果存在就跳出循环
时间: 2024-09-25 15:03:15 浏览: 28
Random-Selector-2.0.rar_Database1.laccdb_random
当你想在Python的Selenium库中通过CSS选择器定位元素,并希望在该元素未出现在页面上时持续查找直到找到为止,可以使用`WebDriverWait`配合`expected_conditions`模块实现这一功能。这里是一个示例:
```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 定义要查找的CSS选择器
css_selector = '.frozen-table'
# 设置等待时间,例如5秒
wait_time = 5
max_attempts = 10 # 设置最大尝试次数
element = None
for _ in range(max_attempts):
try:
element = WebDriverWait(driver, wait_time).until(
EC.presence_of_element_located((By.CSS_SELECTOR, css_selector))
)
break # 如果元素存在,跳出循环
except TimeoutException:
print(f"Element not found after {wait_time} seconds, retrying...")
if element is not None:
# 成功找到元素后,进行后续操作...
else:
print("Element was not found after multiple attempts.")
阅读全文