简体   繁体   中英

Python Selenium WebDriver Stuck at the DropDown and And Not Selecting

I am a python selenium webdriver newbee. I am stuck at a dropdown that has an "onchange" parameter in the html. This is how the inspect window looks:

  • I am trying to first key in the date to the box called "Start"
  • Then move on to the dropdown for the "Schools" (School1, School2 etc). Once I choose the school, the adjacent dropdown (School Levels) will refresh/update/populate with the school levels (middle, high, elem etc) for that school. There is an "onchange=" for this element.
  • Then move on to the School Levels dropdown to choose the level. There is an "onchange=" for this element, too
  • And then move on the the grade level dropdown.

I am stuck at the first dropdown.After I enter the start date, it does move on the dropdown and clicks on it and all three schools get visible but the driver does not select the school and does not move on to the adjacent school level dropdown.

As far as I understand from what I have found online, it most probably has to do with the onchange javascript event but couldn't figure out how to fire the event.

Please help. Thank you in advance.

This is the html block for the start date box:

<div class="inline-block" ><input name="Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$DatePickerStartDate" type="text" size="9" id="Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_DatePickerStartDate" spellcheck="false" style="BACKGROUND:#ff9999;display:inline;" /><input type="image" name="Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$DatePickerStartDate_IB" id="Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_DatePickerStartDate_IB" src="Images/Mindex/WebControls/DatePicker/calendar.jpg" onclick="PopupCal(&#39;Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_DatePickerStartDate&#39;, &#39;Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_DatePickerStartDate&#39;,false,false); return false;" style="border-width:0px;display:inline;vertical-align:middle;" /></div></nobr>

And this is the html block for the two adjacent dropdowns:

<select name="Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingDropDown" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingDropDown\&#39;,\&#39;\&#39;)&#39;, 0)" id="Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_BuildingDropDown">
                                            <option selected="selected" value="3">School1</option>
                                            <option value="4">School2</option>
                                            <option value="5">School3</option>

                                        </select>
<select name="Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingSchoolLevelDropDown" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingSchoolLevelDropDown\&#39;,\&#39;\&#39;)&#39;, 0)" id="Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_BuildingSchoolLevelDropDown">
                                                <option selected="selected" value="4">K-8</option>
                                                <option value="5">High School</option>
                                                <option value="6">Summer School</option>
                                                <option value="7">Middle School</option>
                                                <option value="8">Elementary</option>
    
                                            </select></nobr>

And this is my code:

print("Clicking on student selector icon")
driver.find_element_by_xpath("/html/body/form/div[4]/div[1]/div/div[2]/div/table/tbody/tr/td/table[2]/tbody/tr["+str(r)+"]/td[1]/input").click()
time.sleep(2)
tab_student = driver.find_element_by_id("Template1_Control0_TabHeaderDetails_MenuTabs1_MenuTabStudent")#we can move this to the start of the script
tab_student.click()
time.sleep(3)


icon_add_enrollment = driver.find_element_by_id("Template1_Control0_BottomPanel_IconButtonAdd")#we can move this to the start of the script
icon_add_enrollment.click()
time.sleep(3)

input_startdate = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_DatePickerStartDate")
input_startdate.send_keys("4/21/2010")
time.sleep(2)
print("Start Date entered")


dropdown_school = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_BuildingDropDown")
dropdown_school.select_by_visible_text("School1")

time.sleep(5)
print("School picked")
time.sleep(6)

dropdown_level = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_BuildingSchoolLevelDropDown")
dropdown_level.select_by_visible_text("Elementary")

#or maybe I can use this?  actions.move_to_element(dropdown_building).click().perform()

time.sleep(2)

print("Building picked")
time.sleep(6)

dropdown_grade = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_GradeDropDown")
actions.move_to_element(dropdown_grade).click().perform()
time.sleep(2)
dropdown_grade.select_by_visible_text("1").click()
print("Grade picked")
time.sleep(2)

dropdown_ethnicity = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_ddlEthnicity")
actions.move_to_element(dropdown_ethnicity).click().perform()
time.sleep(2)
dropdown_ethnicity.select_by_visible_text("White").click()
print("Ethnicity picked")
time.sleep(2)

icon_save_enrollment = driver.find_element_by_id("Template1_Control0_BottomPanel_StudentEditEnrollmentAdd_IconButtonAdd")#we can move this to the start of the script
icon_save_enrollment.click()

time.sleep(4)

print("Enrollment added.. Moving on to enrolling the next child")

I also tried move_to_element().click(perform() but still not working.

The onchange javascripts in the html is not showing correctly for some reason so I am attaching them below:

 //for school dropdown: javascript:setTimeout('__doPostBack(\\'Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingDropDown\\',\\'\\')', 0) //for school levels dropdown: javascript:setTimeout('__doPostBack(\\'Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingSchoolLevelDropDown\\',\\'\\')', 0)

Thank you again.

As far as I understand, this is your exact problem:

After I enter the start date, it does move on the dropdown and clicks on it and all three schools get visible but the driver does not select the school and does not move on to the adjacent school level dropdown.

Which means the error occurs when selecting the dropdowns or <select> inputs once the date is properly detected by the onchange.

Ive found this to work on similar cases:

schoolSelector = Select(driver.find_element_by_xpath('//select[@name="Template1$Control0$BottomPanel$StudentEditEnrollmentAdd$BuildingSchoolLevelDropDown"]'))
schoolOptions = schoolSelector.options

then if you want to iterate through each school you may use schoolOptions to get each one with:

for school in range(0,len(schoolOptions)):
    schoolSelector.select_by_index(school)

Edit: Had not noticed you posted all of your code, I think selecting by index and via XPATH is usually more precise. Give it a shot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM