I am trying to scrape this website: http://sekolah.data.kemdikbud.go.id/ I want to select "Jenjang" field, "SMA" value. After that, need to click "Cari Sekolah" button enter image description here
Unfortunately, my code does not work. I manage to select SMA but then can't click "Cari Sekolah" to start the query. Anyone knows how to fix this. Here is my code:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from selenium.webdriver.support.ui import Select
option = webdriver.ChromeOptions()
option.add_argument('--incognito')
webdriver = "/Users/rs26/Desktop/learnpython/web/chromedriver"
driver = Chrome(executable_path=webdriver, chrome_options=option)
url="http://sekolah.data.kemdikbud.go.id/"
driver.get(url)
wait = WebDriverWait(driver,15)
select_element = Select(driver.find_element_by_id("bentuk"))
select_element.select_by_value("SMA")
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Cari Sekolah']"))).click()
2 Answers 2
Please find below solution to select from custom dropdown
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# # Solution 1:
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('http://sekolah.data.kemdikbud.go.id/')
driver.maximize_window()
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "select2-bentuk-container")))
element.click()
list=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='select2-search__field']")))
list.send_keys("SMA")
select=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "select2-results__option")))
select.click()
answered Apr 2, 2020 at 15:00
SeleniumUser
4,1973 gold badges15 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
rs26
Thanks a lot unfortunately the send keys doesnt work. It managed to click the drop down but couldnt select SMA.
SeleniumUser
@rs26: what issue you are facing because I have already verified solution & its working fine for me
rs26
Thanks Dipak. Your code allows me to click the dropdown, but then it stops there and doesnt select "SMA". This is the error: WebDriverException: unknown error: call function result missing 'value'
SeleniumUser
Please try to copy entire solution it's a custom drop down we are sending input value SMA and after that based on class name we are selecting it from list
rs26
Does it have to do with my chrome? the problem is: WebDriverException: disconnected: unable to connect to renderer (Session info: chrome=80.0.3987.162) (Driver info: chromedriver=2.31.488774 (7e15618d1bf16df8bf0ecf2914ed1964a387ba0b),platform=Mac OS X 10.12.6 x86_64)
|
You can use form button[type=submit] css selector to click.
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"form button[type=submit]"))).click()
answered Apr 2, 2020 at 14:48
Sers
12.3k2 gold badges14 silver badges33 bronze badges
1 Comment
rs26
Thanks Sers. The problem is with the dropdown selection because the program stops there. The error is: WebDriverException: unknown error: call function result missing 'value' The submit button works fine on its own.
lang-py