1

This is the HTML for the page I want to use. In the page, I want to iterate through a drop-down menu that looks like this:

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

So, what I want to do is click() every single span option.

This is what I have now:

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)

1 Answer 1

2

Try this code.Hope this helps.

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)

Output:

Antofagasta
Atacama
Arica y parinacota
Tarapacá
Biobio
La araucanía
Maule
Los lagos
Los rios
Magallanes
Aysén
Valparaíso
Metropolitana
Coquimbo
O higgins
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.