Search code examples
pythonseleniumselenium-webdriverweb2py

Select Type not getting selected using python and selenium


I am new to python and using python and selenium to automate testing for a web2py application. In form I have one select option. I am not able to select that value. I tried different ways.

Code for testing is

from selenium import webdriver
import unittest, time
import xlrd

from openpyxl import load_workbook
from itertools import *
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from openpyxl import Workbook
from itertools import izip_longest as zip_longest


import os


class LoginTest(unittest.TestCase):

    def setUp(self):
        self.driver=webdriver.Firefox()
        self.driver.get("http://127.0.0.1:8000/ClinicalManagement/user/login?_next=/ClinicalManagement/admin")
        self.LoginXpath = "//input[@value='Log In']"       

            self.emailFieldID = "auth_user_email"
            self.passFieldID = "auth_user_password"
            self.InboxXpath = "id(':4b')/x:div/x:div[1]/x:span/x:a"





    def test_Basic(self):
        driver = self.driver
        wb=xlrd.open_workbook("cms_testcase.xlsx")
        sheetname = wb.sheet_names()
        sh1 = wb.sheet_by_index(0)


        rows = ['[email protected]']
        col = ['12345678']

        driver.find_element_by_id(self.emailFieldID).send_keys(rows[0])

        print("The user name  is entered")      


        driver.find_element_by_id(self.passFieldID).send_keys(col[0])
        print("The Password  is entered")

        time.sleep(1)

        driver.find_element_by_xpath(self.LoginXpath).click()
            #driver.implicitly_wait(50)
        time.sleep(1)
        driver.find_element_by_id('patient').click()
        time.sleep(1)
        driver.find_element_by_partial_link_text('Add').click()
        driver.find_element_by_id('patient_name').send_keys('Manasa')
        driver.find_element_by_id('patient_email').send_keys('[email protected]')
        driver.find_element_by_id('patient_phone').send_keys('9856254578')
        driver.find_element_by_id('patient_address').send_keys('Near gmail.com')
        driver.find_element_by_id('patient_gender'). select_by_visible_text('Male')





     #def tearDown(self):
     #  self.driver.quit()

     #    try:
        # driver.find_element_by_id(self.emailFieldID).send_keys(self.gmailUserName)

        # self.write_test_case_result('PASS', 'A1')
        # print('Find Username: PASS')
     #    except:
        # self.write_test_case_result('FAIL', 'A1')
        # print('Find Username: FAIL')
  #         try:
        # driver.find_element_by_id(self.passFieldID).send_keys(self.gmailPassword) 
        # self.write_test_case_result('PASS', 'A2')
        # print('Find Password: PASS')
     #    except:
        # self.write_test_case_result('FAIL', 'A2')
        # print('Find Password: FAIL')


     #    try:
        # driver.find_element_by_xpath(self.LoginXpath).click()

        # self.write_test_case_result('PASS', 'A3')
        # print('Find login button: PASS')
     #    except:
        # self.write_test_case_result('FAIL', 'A3')
        # print('Find login button: FAIL')


    # def write_test_case_result(self, result, location):
    #     wb = Workbook()
    #     ws1 = wb.worksheets[0]
    #     ws1.title = 'Test result'
    #     dest_filename = 'Test_Result.xlsx'

    #     while True:
    #   if result == "PASS":
    #       ws1.cell(location).value = "PASSED"
    #       break
    #   else:
    #       ws1.cell(location).value = "FAILED"
    #       break
    #   break
    #     # Save the file
    #     wb.save(filename = dest_filename) 






if __name__ == "__main__":
    unittest.main()

When I am using "driver.find_element_by_id('patient_gender'). select_by_visible_text('Male')" none of the field in the form gets field:

With driver.find_element_by_id for select Gender

When I remove that line the form is filled with values but stops there

without driver.find_element_by_id for select

I have tried with xpath also and find_element_by_value and find_element_by_visible_text and like this - Select(driver.find_element_by_xpath("//select[@name='name']")).select_by_visible_text(" ")

I am extremely sorry for my long and boring description, but could find a better way.


Solution

  • Can you try to select using below syntax:

       WebDriverWait(driver, 10).until(EC.visibility_of_element_located(By.ID("id")))
        select = Select(self.driver.find_element_by_id("id"))
        select.select_by_visible_text('Male')