当前位置: 首页 > 面试题库 >

如何使用Python和Selenium遍历Webelements列表?

聂和宜
2023-03-14
问题内容

我想遍历一个webelements列表并从每个<h2>元素中返回文本,但是我只从第一个元素而不是从其他<li>标记内的其余元素中获取文本,然后存在存在该循环的代码

这是我要从中提取文本的HTML代码的一部分:

<div class="KambiBC-event-page-component__column KambiBC-event-page-component__column--1">



            <ul class="KambiBC-list-view__column">

              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">

                <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">

                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Piete selectate</h2>

                </header>

              </li>

              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">

                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">

                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Another text</h2>

                 </header>

              </li>



              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">

                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">

                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Different text</h2>

                 </header>

             </li>



              <li class="KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in">

                 <header class="KambiBC-bet-offer-category__header" data-touch-feedback="true">

                  <h2 class="KambiBC-bet-offer-category__title js-bet-offer-category-title">Yet another text</h2>

                 </header>

              </li>



            </ul>





      </div>

这是Pyhton代码:

import time

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC



driver = webdriver.Edge("D:\pariuri\python\MicrosoftWebDriver.exe")

driver.implicitly_wait(5)



driver.get("https://www.unibet.ro/betting#filter/football")



try:

    element_present = EC.presence_of_element_located((By.CLASS_NAME, 'KambiBC-event-result__score-list'))

    WebDriverWait(driver, 4).until(element_present)

except TimeoutException:

    print ('Timed out waiting for page to load')



event = driver.find_elements_by_class_name('KambiBC-event-item KambiBC-event-item--type-match')



for items in event:

   link = items.find_element_by_class_name('KambiBC-event-item__link')

   scoruri =  items.find_element_by_class_name('KambiBC-event-item__score-container')



   scor1 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[1]")

   scor2 =  scoruri.find_element_by_xpath(".//li[@class='KambiBC-event-result__match']/span[2]")



   print (scor1.text)

   print (scor2.text)

   if scor1.text == '0' and scor2.text == '0':





        link.click()

        time.sleep(3)





        PlajePariuri = driver.find_elements_by_xpath("//ul[@class='KambiBC-list-view__column']")

        for items in PlajePariuri:

             NumePlaje = items.find_element_by_xpath("//li/header/h2")

             print (NumePlaje.text)

问题答案:

一直在我的脸上,这将打印每个元素的文本,很高兴我能发现

PlajePariuri = driver.find_elements_by_class_name('KambiBC-bet-offer-category KambiBC-collapsible-container KambiBC-expanded KambiBC-bet-offer-category--hidden KambiBC-bet-offer-category--fade-in')


    for items2 in PlajePariuri:

        NumePlaje = items2.find_element_by_class_name('KambiBC-bet-offer-category__title js-bet-offer-category-title')

        print (NumePlaje.text)


 类似资料:
  • 我试图自动化以下场景: < li >访问amazon.com < li >搜索耳机 < li >将第一个结果页面中的所有畅销书添加到购物车中 我为编写此场景所遵循的步骤: < li >访问amazon.com < li >在搜索字段中输入文本“耳机” < li >单击搜索按钮 < li >单击标记为“畅销书”的链接 < li >单击“添加到购物车”按钮 < li >导航回结果页面 < li >单击

  • 因此,页面中充满了许多类似的属性,如下面的两个元素。例子: 我使用PageObject和PageFactory获取元素,并将它们放入如下数组: 问题是:我需要单击其中一个类似的按钮,而不使用索引(因为结果是很难维护)。我想到了一种方法,我可以收集列表中的所有WebElement并返回屏幕上显示的单个WebElement(我乐于接受新想法),因此以下是我尝试过但未成功的两种方法: 我找了很多东西,没

  • 我得到一个使用下面的代码的锚的列表,然后我想去每个链接。我想出了下面的代码,但是在第一个循环之后,我得到了以下异常 org.openqa.selenium.staleElementReferenceException:过时元素引用:元素未附加到页面文档(会话信息:Chrome=55.0.2883.87) 有没有办法做到这一点,而不需要每次返回页面?

  • 问题内容: 我有一个这样的清单清单。 现在,我需要遍历上面的列表并输出一个字符串列表,如下所示(原始列表中没有数字) 问题答案: 准确执行您指定的操作的最简单解决方案是: 这基本上等效于迭代版本: 但是,这并不是遍历具有任意维数的多维列表的通用方法,因为嵌套列表推导/嵌套的for循环可能很难看。但是,对于2或3维列表,您应该放心。 如果您确实决定需要平整3个以上的尺寸,我建议实现一个递归遍历函数,

  • 我正在使用XPath/CSS和Selenium来定位网站上的元素。我想创建一个方法,在这个方法中,我遍历一个定位器列表(XPath/CSS),然后程序选择哪个有效。换句话说,它从定位器1开始-如果定位器存在,它将返回true并存在循环。否则,它将移动到列表中的下一个定位器。一旦用完所有CSS定位器,它就会转到XPath等等。 目前,我正在考虑如下实施: 然后,我计划为每种定位器类型调用此方法(一次

  • 问题内容: 因此,页面充满了很多类似的属性,例如下面的两个元素。例子: 我正在使用PageObject和PageFactory来获取元素,并将它们放入这样的数组中: 问题是这样的:我需要单击这些类似的按钮之一,而不使用索引( 因为事实证明,要维护它很麻烦 )。我想到了一种方法,我可以收集列表中的所有webElement并返回显示在屏幕上的单个webElement(我乐于接受新想法),所以这是我尝试