简体   繁体   English

Selenium 驱动程序不返回所有 web 元素,即使它们是相同的 class

[英]Selenium driver not returning all web elements even though they are the same class

I'm trying to make a web scraper for a webpage using selenium.我正在尝试使用 selenium 为网页制作 web 刮板。

the webpage is https://www.supersport.hr/sport/dan/0/sport/1网页是https://www.supersport.hr/sport/dan/0/sport/1

I am able to get most of the web elements I'm trying to get but my script doesn't return all of them even though they are the same class.我能够获得我想要获得的大部分 web 元素,但我的脚本不会返回所有这些元素,即使它们是相同的 class。

page example:页面示例: 在此处输入图像描述

In this case my script returns all of the above divs, but cuts off at "SRL GRČKA 1."在这种情况下,我的脚本返回所有上述 div,但在“SRL GRČKA 1”处中断。 and doesn't get any leagues below.并且没有低于任何联赛。

the HTML markup for each of these leagues are the same:每个联赛的 HTML 标记都是相同的:

在此处输入图像描述

I'm getting a list of these elements in python like this:我在 python 中得到了这些元素的列表,如下所示:

football_leagues_elements = driver.find_elements_by_css_selector("div.sportska-liga.nogomet")

I've also tried with this code but it returns the same result:我也尝试过使用此代码,但它返回相同的结果:

football_leagues_elements = driver.find_elements_by_xpath("//div[contains(@class, 'sportska-liga-wrap')]//div[contains(@class, 'nogomet')]")

I think all the leagues are loaded to the page at the same time.我认为所有联赛都同时加载到页面上。

My question is why are some divs not included in the webelement list?我的问题是为什么某些 div 不包含在 webelement 列表中?

Any help is welcome.欢迎任何帮助。

The Website is NOT accessible here.However try Induce Explicit wait and wait for all element visible.此处无法访问该网站。但是请尝试 Induce Explicit等待并等待所有元素可见。

football_leagues_elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"div.sportska-liga.nogomet")))

You need to import following libraries.您需要导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

If above doesn't work then try to scroll the page first and then check for elements.如果上述方法不起作用,请先尝试滚动页面,然后检查元素。

#Scroll bottom of the page.
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
#Get the elements.
football_leagues_elements=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"div.sportska-liga.nogomet")))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-Selenium-WebDriver即使驱动程序应该滚动页面,这些元素仍在滚动元素,而无需“单击”它们 - Python-Selenium-WebDriver The page is scrolling past elements without “clicking” on them even though the driver is supposed to Python Selenium 根本不返回值,但也不是 class 中的所有元素 - Python Selenium not returning values at all, but also not for all elements in class 即使使用 Selenium 和 Python 有多个具有相同类名的元素,如何通过类名识别元素 - How to identify an element through classname even though there are multiple elements with the same classnames using Selenium and Python 即使实例由方法返回,Python selenium 驱动程序实例也未定义 - Python selenium driver instance not defined even though instance is returned by method 即使在 Selenium、Python 中休眠,driver.find_elements_by_class_name 也会返回一个空列表 - driver.find_elements_by_class_name returns an empty list even with sleep in Selenium, Python 在Python中使用Selenium单击具有相同类名的所有元素 - Using Selenium in Python to click through all elements with the same class name 将具有相同 class 的所有元素添加到具有 selenium 和 python 的列表 - Add all elements with the same class to list with selenium and python 使用 selenium find_elements_by_xpath 多次返回相同元素的数组,而不是所有元素 - Using selenium find_elements_by_xpath is returning an array of the same element multiple times rather than all elements 具有自定义元类的类的所有子类共享相同的属性,即使它们不应该共享 - All subclasses of a class with a custom metaclass share the same attributes even though they shouldn't 在另一个文件/类中使用相同的 Selenium 驱动程序 - Use same Selenium driver in another file/class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM