Theautocomplete-suggestions
是div
包含所有autocomplete-suggestion
's 的。
这是元素的片段
为了捕捉元素,我f8
在搜索 selenium 时使用了按钮,这样元素就不会消失。
这是可视化的代码片段:
def highlight_element(element):
driver_elem = element.parent
def apply_style(s):
driver_elem.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, s)
original_style = element.get_attribute('style')
apply_style("background: yellow; border: 2px solid red;")
sleep(0.5)
apply_style(original_style)
driver.get("https://www.selenium.dev/documentation/en/")
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#search-by")))
driver.find_element_by_css_selector("#search-by").send_keys("selenium")
WebDriverWait(driver,30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".autocomplete-suggestions .autocomplete-suggestion")))
for ele in driver.find_elements_by_css_selector(".autocomplete-suggestions .autocomplete-suggestion"):
highlight_element(ele)