Selenium find_elements_by_id()不会返回所有元素 [英] Selenium find_elements_by_id() doesn't return all elements
问题描述
我正在尝试使用Selenium(python)在网页上查找某种类型的所有元素.为了简单起见,假设它们的ID为elem_id
.我正在使用以下代码片段来做到这一点:
I am trying to find all elements of a certain kind on a webpage with Selenium (python). For simplicity, let's say their id is elem_id
. I am using the following code snippet to do so:
all_elements = driver.find_elements_by_id("elem_id")
print(str(len(all_elements)))
我知道有问题的网页上有大约3000种此类元素,但是每当我打印all_elements
的长度时,它总是会打印1000
.
I know that there are ~3000 of this kind of element on the webpage in question, but whenever I print the length of all_elements
, it always prints 1000
.
它确实找到了正确的元素类型(我检查过),但是不知何故,它无法一次找到所有元素.它还会随机选择1000个元素,这意味着它既不会选择前1000个元素,也不会最后选择1000个元素.我尝试找出Selenium可以找到多少个元素是否有上限,但似乎没有最大上限.
It definitely finds the right kind of element (I checked), but somehow it doesn't find all of them at once. It also selects the 1000 elements at random, meaning that it neither picks the first 1000 or the last 1000 exclusively. I tried finding out whether there's a cap on how many elements Selenium can find, but there doesn't appear to be a max amount of 1000.
有人知道为什么硒一次只能找到1000个元素吗?我究竟做错了什么?非常感谢你!
Does anyone know why Selenium only finds 1000 elements at a time? What am I doing wrong? Thank you very much!
推荐答案
从根本上讲,您看到的是正确的行为.尽管您知道网页中几乎有3000种这种特定类型的元素,但是:
Fundamentally, you are seeing the correct behavior. Though you are aware there are almost ~3000 of this particular type of element within the webpage, but:
- 所有
id
为elem_id
的元素在不可见 /css_rwd_viewport.asp"rel =" nofollow noreferrer>视口
- All those elements with
id
aselem_id
may not be visible within the Viewport
您可以在
- 其中某些元素可能位于
<iframe>
/<frame>
标记内,并且 WebDriver 实例可能无法从顶级视图中看到这些元素. /li>
- Some of those elements may be within
<iframe>
/<frame>
tags and WebDriver instance may not have the visibility of those elements from the Top Level View.
- 其中一些元素可能会通过延迟加载
保持不可见