首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何遍历Selenium Python中包含<span>对象的<li>

如何遍历Selenium Python中包含<span>对象的<li>
EN

Stack Overflow用户
提问于 2019-02-26 23:09:03
回答 1查看 171关注 0票数 1

这是我要使用的页面的HTML。在页面中,我希望遍历如下所示的下拉菜单:

代码语言:javascript
复制
<ul class="col-24 position-absolute station-filter mobile-region s-padd-0-10">
      <li class=class="flex flex-wrap flex-display-block col-24 bg-white radius-5 overflow-scroll-y station-filter-inside">
          <span class="col-24 display-block padd-15-0 brd-bottom-1 station-select-region">Antofagasta</span>
          <span class="col-24 display-block padd-15-0 brd-bottom-1 station-select-region">Atacama</span>
          <span class="col-24 display-block padd-15-0 brd-bottom-1 station-select-region">Arica y parinacota</span>
          <span class="col-24 display-block padd-15-0 brd-bottom-1 station-select-region">Tarapaca</span> 
          <span class="col-24 display-block padd-15-0 brd-bottom-1 station-select-region">Biobio</span>

因此,我想要做的是click()每个单跨距选项。

这就是我现在所拥有的:

代码语言:javascript
复制
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(r"C:\Users\juanc\OneDrive\Escritorio\chromedriver.exe")
driver.get("https://ww2.copec.cl/stations?check=punto")
driver.find_element_by_xpath("//*[@id='root']/div[1]/div/ul/li[1]/a").click()
result = driver.find_element_by_xpath('//*[@id="root"]/div[1]/div/ul/li[1]/ul/li')
options = result.find_element(By.CLASS_NAME("col-24 display-block padd-15-0 brd-bottom-1 station-select-region"))
for option in options:
    print(option.text)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-26 23:36:57

试试这个code.Hope,这很有帮助。

代码语言:javascript
复制
from selenium import webdriver

driver = webdriver.Chrome(r"C:\Users\juanc\OneDrive\Escritorio\chromedriver.exe")

driver.get("https://ww2.copec.cl/stations?check=punto")
driver.find_element_by_xpath("//*[@id='root']/div[1]/div/ul/li[1]/a").click()
results = driver.find_elements_by_css_selector('span.station-select-region')

for rs in results:
    print(rs.text)

输出:

代码语言:javascript
复制
Antofagasta
Atacama
Arica y parinacota
Tarapacá
Biobio
La araucanía
Maule
Los lagos
Los rios
Magallanes
Aysén
Valparaíso
Metropolitana
Coquimbo
O higgins
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54888556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档