4

I tried using the Select module, but when I do the Element is either not interactable or it is "not visible". Here are the relevant codes.

HTML

    < head >
    < script >

        function onChangeCardType() {
            var value = $('#card_type').val();
            $('#img_' + value).siblings().hide();
            $('#img_' + value).show();
        }

    </script>

</head>

<body>
    <table>
        <thead>

            <tr>
                <th align="left">Card type</th>
                <td colspan="2" style="font-size:12px;">
                    <select name="requestDTO.vpc_card" id="card_type" onchange="onChangeCardType()"
                            class="select required"
                            style="width: 342px; font-size:12px;">
                        <option value="Amex" >American Express</option>
                        <option value="Mastercard" >MasterCard</option>
                        <option value="Visa" >Visa</option>
                        <option value="JCB" >JCB</option>
                    </select>
                    <a class="ui-selectmenu ui-widget ui-state-default select required ui-selectmenu-dropdown ui-corner-all" id="card_type_button_435" role="button" href="#" aria-haspopup="true" aria-owns="card_type_menu_435" aria-expanded="false" tabindex="0" style="width: 336px;"><span class="ui-selectmenu-status">Visa</span><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span></a>
                        <span class="ui-selectmenu-status">Visa</span>

                        <span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span>
                   
                </td>
            </tr>

         </thead>
    </table>
</body>

Code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import Select

#testing on a website that's not public yet, so I won't show the set-up here,but if it's required I can too

cardtype = Select(driver.find_element_by_id("card_type"))
cardtype.select_by_value("Mastercard")
sleep(1)
driver.implicitly_wait(2)


Using Firefox: ElementNotInteractableException:
Element <option> could not be scrolled into view


Using Chrome:
ElementNotVisibleException: element not visible: 
Element is not currently visible and may not be manipulated

# sleep nor implicitly_wait doesn't help too...

I also tried just clicking on the box (not using the select tag, I could click on it using the class="ui-selectmenu", but .send_keys(KEYS.ARROW_DOWN) doesn't work (gives an AttributeError).

Is there a way I can identify the text in an option and click on it without using the Select module? Or is there a way I can get the Select module to work in this case?

2
  • Malformed HTML. The corresponding starting markup is missing for the second <a> tag Commented Jun 20, 2018 at 4:19
  • @DebanjanB Thanks for noticing! The reason why it's malformed is because I had to manually paste in the first <a> section and the two <span> classes below it (somehow not all the HTML code in the "element" box is shown in the page source?) so I just deleted the second </a> because I couldn't figure out where the other starting <a> should be at... Commented Jun 20, 2018 at 7:54

6 Answers 6

5

thanks so much for the responses! Unfortunately, the problem was really not with wait in this case:/

What did work for me however, was Action Chains. Actions Chains works because you don't have to target an element. So in my post I mentioned I could click the dropdown list, and that down arrows didn't work because it gave an AttributeError. However, that was because I tried targeting the element!

So here's the answer that worked for me:

cardtype = driver.find_elements_by_class_name("ui-selectmenu-status")
cardtype.click()

actions = ActionChains(driver)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ENTER)
actions.perform()
Sign up to request clarification or add additional context in comments.

Comments

1

You can use select_by_index. I personally recommend instead of value

cardtype = Select(driver.find_element_by_id("card_type"))
cardtype.select_by_index(1)  // 0 - AMEX , 1 - MasterCard and so on

1 Comment

@DebanjanB - Yes, you're correct. My Bad. I will edit it.
1

You can try this code :

WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.ID, "card_type")))  
cardtype = Select(driver.find_element_by_id('card_type'))
cardtype.select_by_value("Mastercard")  
#OR  

cardtype.select_by_visible_text('MasterCard')  

Hope this will be helpful.

1 Comment

Thanks for the reply! Unfortunately, it didn't work and after twenty seconds this error appeared raise TimeoutException(message, screen, stacktrace) TimeoutException and this is a form, so the drop-down bar will always be there. Is there a reason why we should use visibility of element_located?
0

Try this.

your_choice=driver.find_element_by_xpath("//select[@id='card_type']/option[@value='Mastercard']")
your_choice.click()

Comments

0

You need to wait for a while(2 Seconds), Before Select actions. You can use Explicit Wait or sleep. That will solve Firefox as well as Chrome problem.

Explicit Wait :

from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "card_type")))

Sleep:

import time
time.sleep( 5 )

Comments

0

First click on the dropdown,

accountspageobj.select_user_dropdown_on_assign_eventpage().click();

Use the below actions class to select the values in a dynamic dropdown.

Actions s= new Actions(driver);
s.sendKeys(accountspageobj.select_user_dropdown_on_assign_eventpage(), Keys.chord(Keys.DOWN,Keys.DOWN,Keys.ENTER)).build().perform();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.