Home > Net >  Selenium Python nested dropdowns
Selenium Python nested dropdowns

Time:09-05

So I'm trying to test a nested dropdown with Selenium Python using the conditional statements like so: enter image description here

if d_Biz == 'Marketing':
                biz_choose = driver.find_element("xpath", "//span[@id='biz']")
                biz_choose.click()
                time.sleep(1)
                gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                gwrapper.find_element("xpath", "//li[normalize-space()='Marketing']").click()
                time.sleep(1)

                if d_Subbiz_1 == 'GMV':
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='GMV']").click()
                    time.sleep(1)
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_2)   "']").click()
                    time.sleep(1)

                elif d_Subbiz_1 == 'CLM':
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='CLM']").click()
                    time.sleep(1)
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_2)   "']").click()
                    time.sleep(1)

                else:
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_1)   "']").click()

            elif d_Biz == 'Lzdpay':
                biz_choose = driver.find_element("xpath", "//span[@id='biz']")
                biz_choose.click()
                time.sleep(1)
                gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                gwrapper.find_element("xpath", "//li[normalize-space()='Lzdpay']").click()

                if d_Subbiz_1 == 'Wallet':
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='Wallet']").click()
                    time.sleep(1)
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_2)   "']").click()
                    time.sleep(1)

                elif d_Subbiz_1 == 'BAR':
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='BAR']").click()
                    time.sleep(1)
                    gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_2)   "']").click()
                    time.sleep(1)

            else:
                biz_choose = driver.find_element("xpath", "//span[@id='biz']")
                biz_choose.click()
                time.sleep(1)
                gwrapper = driver.find_element("xpath", "//div[@data-tag='gateway-wrapper']")
                gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Biz)   "']").click()

As you can see, I'm writing it 1 by 1. It works fine but then when the data is

Biz: Marketing
Sub Biz 1: Engagement
Sub Biz 2: Engagement

or

Biz: Marketing
Sub Biz 1: Branding
Sub Biz 2: Branding

it gives an error saying :

Message: element click intercepted: Element <span data-meta="Field" id="purpose"  tabindex="0">...</span> is not clickable at point (526, 335). Other element would receive the click: 
<li >...</li>

In conclusion, this code works fine if the Sub Biz 1 and Sub Biz 2 is not the same value. Any solutions? By the way, I'm new to selenium.

[[edit]] I also tried coding like this:

if d_Biz is not None:
                biz_choose = driver.find_element("xpath", "//span[@id='biz']")
                biz_choose.click()
                time.sleep(1)
                gwrapper = driver.find_element("xpath", "//div[@class='next-cascader-menu-wrapper'][1]")
                gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Biz)   "']").click()
                if d_Subbiz_1 is not None:
                    gwrapper = driver.find_element("xpath", "//div[@class='next-cascader-menu-wrapper'][2]")
                    gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_1)   "']").click()
                    time.sleep(1)
                    if d_Subbiz_2 is not None:
                        gwrapper = driver.find_element("xpath", "//div[@class='next-cascader-menu-wrapper'][3]")
                        gwrapper.find_element("xpath", "//li[normalize-space()='"   str(d_Subbiz_2)   "']").click()
                        time.sleep(1)

With this code, it works fine for the duplicated value for d_Subbiz_1 and d_Subbiz_2 while the error shows when there are no d_Subbiz_1 or d_Subbiz_2 value

CodePudding user response:

The problem is when Sub Biz 1 and Sub Biz 2 have the same value, as you' re location them using text, the locator works for both of them and you are pointing to first one (parent) again. try using indexes in your locators like "//li[normalize-space()='" str(d_Subbiz_2) "'][1]" or choose different unique locators for all the drop down list items. I also suggest to use hashtable (dictionary) for this part. For example

def choose_biz_dropdowns(d_biz = None, subbiz1 = None, subbiz2 = None):
    d_biz_dict = {'Marketing': 'XPATH', 'Customer Care': 'XPATH', 'Commercial': 'XPATH', 'People': 'XPATH', 'Other': 'XPATH', 'Lzdpay': 'XPATH'}
    d_subbiz_1_dict = {'GMV': 'XPATH', 'CLM': 'XPATH', 'Engagement': 'XPATH', 'Branding': 'XPATH', 'Partnership': 'XPATH', 'Lucky Voucher': 'XPATH'}
    d_subbiz_2_dict = {'Engagement': 'XPATH', 'UAT Test': 'XPATH',}
    if d_biz is not None:
        driver.find_element("xpath", d_biz_dict[d_biz])
    if subbiz1 is not None:
        driver.find_element("xpath", d_subbiz_1_dict[subbiz1])
    if subbiz2 is not None:
        driver.find_element("xpath", d_subbiz_2_dict[subbiz2])

if you want to not do the second and third drop down (for example "Customer Care"), you can simply leave the argument value as None. In case you want to continue to the last dropdown steps, you should pass all 3 elements name.

CodePudding user response:

The biggest problem i see is that you use 'global' selectors. // Selects nodes in the document from the current node that match the selection no matter where they are.

Depending on the exact html output of your dropdown, you should select the direct child "Marketing" element with

gwrapper.find_element("xpath", "li[normalize-space()='Marketing']")
// or:
gwrapper.find_element("xpath", "ul/li[normalize-space()='Marketing']")

Also see: https://www.selenium.dev/documentation/webdriver/elements/finders/#find-elements-from-element

  • Related