I'm trying to learn selenium in python. Everything was fine until I had issues like this. I.E I have 2 element selections:
Balance Type Selection Element:
<select id="balance_type_id" name="balance_type_id">
<option selected="" disabled="">-- Pilih --</option>
<option value="1, Deposit">Deposit</option>
<option value="3, Pengembalian Dana">Pengembalian Dana</option>
<option value="4, Sharing Profit Proyek">Sharing Profit Proyek</option>
</select>
Project Selection Element:
<select id="project_option_selection" name="project_id"></select>
So if I select the Pengembalian Dana selection, the Project Selection Element will show option element:
<select id="project_option_selection" name="project_id">
<option selected="" disabled="">-- Pilih --</option>
<option value="1">Project 1</option>
<option value="2">Project 2"</option>
</select>
It always throws an error when the code to perform an action on this element. I've tried using explicit wait. But is there something wrong with the way I wear it? Is there any solution for me to be able to select action on Project Selection.
I tried to use expected condition text_to_be_present_in_value
driver.get("http://127.0.0.1:8000/cash-mutations/create/incoming-balance")
balance_type_selection = driver.find_element(by=By.NAME, value="balance_type_id")
Select(balance_type_selection).select_by_visible_text("Pengembalian Dana")
try:
wait.until(EC.text_to_be_present_in_value((By.NAME, 'balance_type_id', "3")))
print("Pendanaan Proyek is selected")
# Select(driver.find_element(by=By.NAME, value="project_id")).select_by_value("1")
except:
print('gagal')
CodePudding user response:
Actually there is no "value" of "Balance Type Selection Element" in your case, they are just options under a <select>
tag.
So you never get any response by wait.until(EC.text_to_be_present_in_value((By.NAME, 'balance_type_id', "3")))
Try this in your code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
driver.get("http://127.0.0.1:8000/cash-mutations/create/incoming-balance")
balance_type_selection = driver.find_element(by=By.NAME, value="balance_type_id")
Select(balance_type_selection).select_by_visible_text("Pengembalian Dana")
try:
wait(driver, timeout=10).until(EC.presence_of_element_located(
(By.XPATH, '//*[@id="project_option_selection"]/option[1]')))
print("Pendanaan Proyek is selected")
# Select(driver.find_element(by=By.NAME, value="project_id")).select_by_value("1")
except:
print('gagal')
CodePudding user response:
I have solve this problem by doing this:
try:
wait.until(EC.text_to_be_present_in_element((By.XPATH, '//*[@id="balance_type_id"]/option[3]'), 'Pengembalian Dana'))
Select(driver.find_element(by=By.NAME, value="project_id")).select_by_value("1")
except:
print('gagal')
So the first thing I did was select the option tag for Pendanaan Proyek by using text_to_be_present_in_element
. So I selected the option for Pendanaan Proyek by XPATH. If Pendanaan Proyek is selected, then do the next script to select the project.