Select 使用 python 和 selenium 未选择类型

Select Type not getting selected using python and selenium

我是 python 的新手,使用 python 和 selenium 来自动测试 web2py 应用程序。在表格中,我有一个 select 选项。我无法 select 该值。我尝试了不同的方法。

测试代码是

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 = ['mhmi.saan@gmail.com']
        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('manasa@gmail.com')
        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()

当我在表单中使用 "driver.find_element_by_id('patient_gender'). select_by_visible_text('Male')" none 字段时获取字段:

当我删除该行时,表格中填满了值但停在那里

我也尝试过使用 xpath 以及 find_element_by_value 和 find_element_by_visible_text 并且像这样 - Select(driver.find_element_by_xpath("//select[@name='name']")).select_by_visible_text(" ")

对于我冗长乏味的描述,我深感抱歉,但我可以找到更好的方法。

你能尝试 select 使用以下语法吗:

   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')

这对我有用

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
from selenium.webdriver.support.ui import Select



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"
        self.w2p = "//input[@value='w2p_keywords']"

        self.patient_name="//input[@id='patient_name']"
        self.patient_email="//input[@id='patient_email']"
        self.patient_phone="//input[@id='patient_phone']"
        self.patient_address="//input[@id='patient_address']"
        self.patient_gender="//select[@id='patient_gender']"
        self.patient_dob="//input[@id='patient_dob']"


    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 = ['mami.san@gmail.com']
        col = ['1234678']

        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('maa@gmail.com')
        driver.find_element_by_id('patient_phone').send_keys('9856254578')
        driver.find_element_by_id('patient_address').send_keys('Near gmail.com')

        WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id('patient_gender'))
            print(driver.title)



        select = Select(driver.find_element_by_id("patient_gender"))
        select.select_by_visible_text('Male')

        driver.find_element_by_id('patient_dob').send_keys('2/2/2016')

        WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id('patient_gender'))
            print(driver.title)

        select = Select(driver.find_element_by_id("patient_blood_group"))
        select.select_by_visible_text('O+')








     #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()