[英]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。
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.