Home > Software engineering >  Option element not found while selecting from the dropdown
Option element not found while selecting from the dropdown

Time:02-14

Not able to click the dropdown even after providing correct xpath. Please find below the code for your reference. Here I am using Select class.

BasePage:

def SelectSumInsured(self,sumvalue):
    sumdropdown = Select(self.driver.find_element(By.XPATH, "//select[@formcontrolname = 'income']"))
    suminsured = sumdropdown.select_by_value(sumvalue)
    return suminsured

Page Object Screen:

class HealthInsurancePage(BasePage):

    def __init__(self, driver):
        super().__init__(driver)


    def SelectSum(self,sumvalue):
        self.SelectSumInsured(sumvalue)

Test Script:

Now in the testscript, I have used pytest parameterized for the data driven approach which should select the dropdown value as per my data I have provided in the excel sheet.

class Test_RSA_Health(BaseTest):
@pytest.mark.parametrize("pin,sumvalue", dataProvider.get_data("excelsheetname"))
def test_RSA_Health(self,pin,sumvalue):
    home = HomePage(self.driver)
    healthinsuranepage = home.SelectHealth()
    self.VerifyPresence_PinCodeTextBox()
    healthinsuranepage.landing_page()
    healthinsuranepage.InputPin(pin)
    healthinsuranepage.SelectSum(str(sumvalue))

Error: During the execution, I have seen that the dropdown is not even clicked and I am getting the below error for your reference

selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000

But if I use simple click method using the same xpath, the drop down is clicking successfully.

self.driver.find_element(By.XPATH, "//select[@formcontrolname = 'income']").click

My question is why the dropdown is not clicking using the Select class?

Full error trace for your reference if you need:

self = <TestCases.test_RSA_Health.Test_RSA_Health object at 0x000002A63A562DF0>, pin = 560008, sumvalue = 700000, mobileno = 9836680067, selfage = 30, fullname = 'Apratim Chaudhuri'
email = '[email protected]', firstname = 'Apratim', lastname = 'Chaudhuri', dob = '1/2/1992', income = 1000000, pan = 'AMHPC9725D', designation = 'QA', add1 = 'GD'
add2 = 'SaltLake', height = 180, weight = 75, nomfirstname = 'testnomineefirst', nomlastname = 'testnomineelast', nomdob = '1/2/1992'

    @pytest.mark.parametrize("pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob", dataP
rovider.get_data("rsa_health"))
    def test_RSA_Health(self,pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob):
        home = HomePage(self.driver)
        healthinsuranepage = home.SelectHealth()
        self.VerifyPresence_PinCodeTextBox()
        healthinsuranepage.landing_page()
        healthinsuranepage.InputPin(pin)
>       healthinsuranepage.SelectSum(str(sumvalue))

test_RSA_Health.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
    self.SelectSumInsured(sumvalue)
..\Pages\BasePage.py:79: in SelectSumInsured
    suminsured = sumdropdown.select_by_value(sumvalue)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.select.Select object at 0x000002A63A56D7F0>, value = '700000'

    def select_by_value(self, value):
        """Select all options that have a value matching the argument. That is, when given "foo" this
           would select an option like:

           <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

           throws NoSuchElementException If there is no option with specified value in SELECT
           """
        css = "option[value =%s]" % self._escapeString(value)
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        matched = False
        for opt in opts:
            self._setSelected(opt)
            if not self.is_multiple:
                return
            matched = True
        if not matched:
>           raise NoSuchElementException("Cannot locate option with value: %s" % value)
E           selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000

..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py:87: NoSuchElementException
---------------------------------------------------------------------------------- Captured log call ----------------------------------------------------------------------------------
INFO     Pages.BasePage:BasePage.py:27 Clicking on an Element health_XPATH
INFO     Pages.BasePage:BasePage.py:60 Typing in an Element pin_XPATH entered the value as : 560008
_ Test_RSA_Health.test_RSA_Health[560008-500000-9836680067-30-Apratim [email protected]/2/1992-1000000-AMHPC9725D-QA-GD-SaltLake-180-75-testn
omineefirst-testnomineelast-1/2/1992] _

self = <TestCases.test_RSA_Health.Test_RSA_Health object at 0x000002A63A63D4F0>, pin = 560008, sumvalue = 500000, mobileno = 9836680067, selfage = 30, fullname = 'Apratim Chaudhuri'
email = '[email protected]', firstname = 'Apratim', lastname = 'Chaudhuri', dob = '1/2/1992', income = 1000000, pan = 'AMHPC9725D', designation = 'QA', add1 = 'GD'
add2 = 'SaltLake', height = 180, weight = 75, nomfirstname = 'testnomineefirst', nomlastname = 'testnomineelast', nomdob = '1/2/1992'

    @pytest.mark.parametrize("pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob", dataP
rovider.get_data("rsa_health"))
    def test_RSA_Health(self,pin,sumvalue,mobileno,selfage,fullname,email,firstname,lastname,dob,income,pan,designation,add1,add2,height,weight,nomfirstname,nomlastname,nomdob):
        home = HomePage(self.driver)
        healthinsuranepage = home.SelectHealth()
        self.VerifyPresence_PinCodeTextBox()
        healthinsuranepage.landing_page()
        healthinsuranepage.InputPin(pin)
>       healthinsuranepage.SelectSum(str(sumvalue))

test_RSA_Health.py:17:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\Pages\HealthInsurancePage.py:23: in SelectSum
    self.SelectSumInsured(sumvalue)
..\Pages\BasePage.py:79: in SelectSumInsured
    suminsured = sumdropdown.select_by_value(sumvalue)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.select.Select object at 0x000002A63A56DC40>, value = '500000'

    def select_by_value(self, value):
        """Select all options that have a value matching the argument. That is, when given "foo" this
           would select an option like:

           <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

           throws NoSuchElementException If there is no option with specified value in SELECT
           """
        css = "option[value =%s]" % self._escapeString(value)
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        matched = False
        for opt in opts:
            self._setSelected(opt)
            if not self.is_multiple:
                return
            matched = True
        if not matched:
>           raise NoSuchElementException("Cannot locate option with value: %s" % value)
E           selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 500000

..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py:87: NoSuchElementException

As per request, following is the element for your reference

<select _ngcontent-tpo-c7="" formcontrolname="income" >
<!---->
<option _ngcontent-tpo-c7="" >100000</option>
<option _ngcontent-tpo-c7="" >200000</option>
<option _ngcontent-tpo-c7="" >300000</option>
<option _ngcontent-tpo-c7="" >400000</option>
<option _ngcontent-tpo-c7="" >500000</option>
<option _ngcontent-tpo-c7="" >700000</option>
<option _ngcontent-tpo-c7="" >1000000</option>
<option _ngcontent-tpo-c7="" >1500000</option>
<option _ngcontent-tpo-c7="" >2000000</option>
<option _ngcontent-tpo-c7="" >2500000</option>
<option _ngcontent-tpo-c7="" >3000000</option>
<option _ngcontent-tpo-c7="" >4000000</option>
<option _ngcontent-tpo-c7="" >5000000</option>
<option _ngcontent-tpo-c7="" >7500000</option>
<option _ngcontent-tpo-c7="" >10000000</option>
</select>

The xpath I have written is

//select[@formcontrolname = 'income'] `or` `//select[@class='ng-pristine ng-valid ng-touched']`

CodePudding user response:

This error message...

raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 700000

and this error message...

raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 500000

...implies that the element have no descendent <option> elements with value attribute as 700000 or 500000.

You need to crosscheck if the <select> have the following structure:

<select formcontrolname="income" ...>
    <option value="700000" ...>foo</option>
    <option value="500000" ...>bar</option>
</select>

Conclusion

As you mentioned seen that the dropdown is not even clicked, this implies that the locator strategies doesn't identifies the desired select element uniquely.

  • Related