我無法使用下面的 Python 代碼選擇選擇選項。我嘗試參考許多問答,例如 select、execute_script ......但它們仍然無法正常作業。
在此處輸入影像描述
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()
uj5u.com熱心網友回復:
使用此代碼,您無需單擊即可將當前選擇的選項更改為所需的選項。
# 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)
uj5u.com熱心網友回復:
您首先單擊下拉選單,然后單擊要選擇的值,如下所示
進口
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebElement
選擇期望值
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);
uj5u.com熱心網友回復:
我的猜測是您無法單擊該元素,因為它不可見。
要使其可見,您必須按照@Akzy 的建議首先單擊下拉選單
driver = webdriver.Chrome()
url: str = "http://kras.seoul.go.kr/land_info/info/landprice/landprice.do"
driver.get(url)
dropdown_element = driver.find_element_by_id("sggnm")
# Open the dropdown menu first
dropdown_element.click()
option_element = driver.find_element_by_xpath('//*[@id="sggnm"]/option[11]')
# Now the options are visible we want to click the option in the list
option_element.click()
這未經測驗(對于此頁面),但應該適用于通用網頁。
還有其他方法可以做到這一點,例如將向下鍵發送 n 次到下拉元素獲取第 n 個選項元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/478797.html
