Home>

I want to use the variable i of the for statement as the value of the option [value] of xpath obtained from the website.
However, it seems impossible to insert i directly, so I would appreciate it if you could tell me something else.

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="selectPeriod"]/div/div[2]/div[2] /div[2]/select[1]/option[i]"}
  (Session info: chrome=83.0.4103.97)
Applicable source code
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
driver = webdriver.Chrome()
time.sleep(1)
#Detailed setting of data to download ============================================ ===================================
# Select item
driver.find_element_by_id('elementButton').click()
time.sleep(1)
# Select temperature, precipitation, wind speed
driver.find_element_by_xpath('//*[@id="aggrgPeriod"]/div/div[1]/div[1]/label/input').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="temperature"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="precipitation"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="wind direction/velocity"]').click()
time.sleep(1)
# Choose display options
driver.find_element_by_id('optionButton').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="selectOp"]/div[1]/div/div[2]/p/label/input').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="selectOp"]/div[2]/div/div[1]/p/label/input').click()
time.sleep(1)
#================================================== =================================================
# Choose a period
for i in range(2,12):
    driver.find_element_by_id('periodButton').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="selectPeriod"]/div/div[1]/div[1]/label/input').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="selectPeriod"]/div/div[2]/div[2]/div[2]/select[1]/option[i]').click() # From the year
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="selectPeriod"]/div/div[2]/div[2]/div[2]/select[2]/option[i]').click() # Up to year
    time.sleep(1)
Supplemental information (FW/tool ​​version, etc.)

The code isMeteorology x Python-Automatic acquisition of AMeDAS point data-Created with reference to.
I would like to specify the number of years on the due date designation screen when creating past data of AMeDAS from the JMA website with a for statement.

  • Answer # 1

    # -*- coding: utf-8 -*-
    from selenium import webdriver
    import time
    from selenium.webdriver.support.ui import Select
    driver = webdriver.Chrome()
    time.sleep(1)
    #Detailed setting of data to download ============================================ ===================================
    # Select item
    driver.find_element_by_id('elementButton').click()
    time.sleep(1)
    # Select temperature, precipitation, wind speed
    driver.find_element_by_xpath('//*[@id="aggrgPeriod"]/div/div[1]/div[1]/label/input').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="temperature"]').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="precipitation"]').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="wind direction/velocity"]').click()
    time.sleep(1)
    # Choose display options
    driver.find_element_by_id('optionButton').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="selectOp"]/div[1]/div/div[2]/p/label/input').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="selectOp"]/div[2]/div/div[1]/p/label/input').click()
    time.sleep(1)
    #================================================== =================================================
    # Choose a period
    for i in range(2,12):
        driver.find_element_by_id('periodButton').click()
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="selectPeriod"]/div/div[1]/div[1]/label/input').click()
        time.sleep(1)
        start_year = Select(driver.find_element_by_name('iniy')) # from year
        start_year.select_by_index(i)
        time.sleep(1)
        stop_year= Select(driver.find_element_by_name('endy')) # up to year
        stop_year.select_by_index(i)
        time.sleep(1)
    driver.quit()
    time.sleep(1)