【问题标题】:How to find multiply elements with same path in one page using Selenium Xpath如何使用 Selenium Xpath 在一页中查找具有相同路径的多个元素
【发布时间】:2017-12-03 23:57:50
【问题描述】:

我尝试使用 python 用 selenium 提取“我想要的文本”数据。

我在下面使用的代码只拉第一个。

e = driver.find_element_by_xpath('.//*[@class = "sth3"]/span')
e.text

我该怎么办? 页面来源如下:

<li id = "12345" class = "sth">
 <div class = "sth1">
  <div class = "sth2">
   <a>
    <div class = "sth3">
     <span class = "sth4">Text I Want</span>
<li id = "12345" class = "sth">
 <div class = "sth1">
  <div class = "sth2">
   <a>
    <div class = "sth3">
     <span class = "sth4">Text I Want</span>

【问题讨论】:

    标签: python html selenium xpath webdriver


    【解决方案1】:

    尝试使用以下代码:

    text_list = [e.text for e in driver.find_elements_by_xpath('.//*[@class = "sth3"]/span')]
    

    这应该允许您从所有必需元素中获取文本内容列表

    【讨论】:

      【解决方案2】:
      // First find out the elements having xpath span and store it in the list of elements.  
        List<WebElements> list_ele = driver.findElements(By.xpath("//span"));
      
      then with the help of for loop print value of each span text.
      
          for(webElement x : list){
              system.out.println(x.getText());
          }
      

      【讨论】:

      • 请使用edit 链接解释这段代码是如何工作的,不要只给出代码,因为解释更有可能帮助未来的读者。另请参阅How to Answer
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多