简体   繁体   English

使用 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

I'm trying to write a bot to reserve a specific time for a specific activity on a gym website.我正在尝试编写一个机器人来为健身房网站上的特定活动保留特定时间。 The gym's website looks like this:健身房的网站是这样的:

健身房网站

with multiple instances of various activities like "Basketball Room," "Gym Main Access," "Swimming Pool," and other activities.具有各种活动的多个实例,例如“篮球室”、“健身房主通道”、“游泳池”和其他活动。 My goal is to select every "block" of information and store it in an array.我的目标是 select 的每个“块”信息并将其存储在一个数组中。 Then, I can iterate through the array and separate out activities by availability, type, and time.然后,我可以遍历数组并按可用性、类型和时间分离出活动。

My code looks like this:我的代码如下所示:

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

listOfClasses = browser.find_elements_by_xpath("//div[@ng-repeat='class in value']")
for classBlock in listOfClasses:
    n = classBlock.find_element_by_xpath("//h1[@class='class-title ng-binding']")
    t = classBlock.find_element_by_xpath("//div[@class='row class-details']/div[@class='col-lg-3 ng-binding'][2]")
    print("Class:",n.text,"Time of class",t.text)

There are 142 possible classes to choose from on the page.页面上有 142 个可能的类可供选择。 When I run my script, listOfClasses is 142 items long, but when I inspect the values of n (the name of the class) and t (the time of the class), indexes 0-3 have no value, and indexes 4-141 are all just value of the first element on the page.当我运行我的脚本时,listOfClasses 有 142 个项目长,但是当我检查 n(类的名称)和 t(类的时间)的值时,索引 0-3 没有值,索引 4-141都是页面上第一个元素的值。 I only have instances of the Basketball Room from 5:00 AM to 6:00 AM.我只有从早上 5:00 到早上 6:00 的篮球室实例。

It would seem that either my initial array of classes is being occupied only by the first element on the page, or else that I'm searching through the same item in listOfClasses each time.似乎我的初始类数组仅被页面上的第一个元素占用,或者我每次都在 listOfClasses 中搜索相同的项目。 What am I doing incorrectly?我做错了什么?

So turns out, I'm not the first person to make this error.事实证明,我不是第一个犯这个错误的人。 I found this useful related question that I'm inadvertently duplicating.我发现我无意中重复了这个有用的相关问题。 Essentially the "//" option tells selenium to begin searching from the root of the document.本质上,“//”选项告诉 selenium 从文档的根目录开始搜索。 Instead, I want ".//" which tells it to search only through child nodes of the current node in its results.相反,我想要“.//”,它告诉它仅在其结果中搜索当前节点的子节点。

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

相关问题 python/Selenium --> find_elements_by_xpath 方法找不到所有元素 - python/Selenium --> find_elements_by_xpath method not finding all elements Python Selenium 网页抓取:find_elements_by_xpath 返回一个空列表 - Python Selenium Webscraping: find_elements_by_xpath returning an empty list 硒的问题-find_elements_by_xpath或find_elements_by_tag - issue with selenium - find_elements_by_xpath or find_elements_by_tag 无法使用python中的硒中的find_elements_by_xpath检索元素列表 - Unable retrieve a list of elements using find_elements_by_xpath in selenium using python WebDriver在find_elements_by_xpath上等待 - WebDriverWait on find_elements_by_xpath find_elements_by_xpath 不包含 3 次特定字符串 - find_elements_by_xpath that do not contains 3 times a specific string find_elements_by_xpath 的第二次迭代在 selenium python 中给出错误 - Second iterative of find_elements_by_xpath gives error in selenium python Selenium Webdriver 和 Jupyter Notebook。 find_elements_by_xpath 不在同一个单元格中工作 - Selenium Webdriver and Jupyter Notebook. find_elements_by_xpath not working in same cell 如何用硒的find_elements_by_xpath捕获列表中几个元素的内容? - How to catch in a list several element's content with selenium's find_elements_by_xpath? Python:Selenium 驱动程序 find_elements_by_xpath:问题 - Python: Selenium Driver find_elements_by_xpath: Issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM