如何在一个页面中找到多个元素而不必每次都重读内容?

2024-04-25 19:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用

find_element_by_css_selector

从一个网页上读取大约400个变量,而且速度非常慢。你知道吗

我分析了代码并注意到,对于每个find_元素,selenium正在访问httplib,并且非常确定它正在重新读取页面-我认为这就是运行速度非常慢的原因。你知道吗

有没有办法告诉selenium先读一遍然后再使用

 find_element_by_css_selector

只是为了分析元素?(页面内容在读取之间不会改变)


Tags: 代码元素网页内容byselenium原因页面
1条回答
网友
1楼 · 发布于 2024-04-25 19:46:15

W3 Schools CSS Selectors

Chaining Css Selectors

Selector for all div and all p elements 'div, p'

Selenium Python Readthedocs Locating Elements By CSS Selectors

Use this when you want to locate an element by CSS selector syntax.

# find single div element with class item-container.
first_div = driver.find_element_by_css_selector('div.item-container') 

# find single img element with class item-image.
first_img = driver.find_element_by_css_selector('img.item-image') 

# find multiple div elements with class item-container.
all_item_divs = driver.find_elements_by_css_selector('div.item-container') 

# find multiple img elements with class item-container.
all_item_images = driver.find_elements_by_css_selector('img.item-image')

# find multiple div elements with class item-container,
#   and img elements with class item-container.
all_item_elements = driver.find_elements_by_css_selector('div.item-container, img.item-image')

相关问题 更多 >

    热门问题