Home > database >  My Python selenium code cannot select a select option
My Python selenium code cannot select a select option

Time:05-19

I cannot select a select option using below Python code.. I have tried to refer many Q&A such as select, execute_script... but they are still not working.

enter image description here

import time
from selenium import webdriver

browser = webdriver.Chrome()

seoul_url = 'http://kras.seoul.go.kr/land_info/info/landprice/landprice.do'
browser.get(seoul_url)

time.sleep(1)

browser.find_element_by_xpath('//*[@id="sggnm"]/option[11]').click()

CodePudding user response:

With this code you can change the currently selected option to the desired one without doing a click.

# webelement containing the currently selected option
option1 = driver.find_element(By.XPATH, '//*[@id="sggnm"]/option[1]')
# string with the text of the 11th option
option11 = driver.find_element(By.XPATH, '//*[@id="sggnm"]/option[11]').text
# replace the current option with the 11th option
driver.execute_script("var el = arguments[0]; arguments[1].innerText = el", option11, option1)

CodePudding user response:

You have first click on dropdown and then click on the value that you wanted to select like below

Import

import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebElement

select the desire value

WebElement element = browser.find_element_by_xpath("//option[contains(text(),'동대문구')]"); // you can choose the value you want
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
  • Related