2

I can't get Python to select "Buy" from the drop down menu:

<a href="" role="button" aria-pressed="false" class="td-wb-dropdown__item-content" data-ng-click="tdWbDropdownItemController.select()" data-td-wb-transclude=""> <span data-ng-bind="'trading.order.common.ACTION_' + item | translate">Buy</span> </a>

My code:

select = Select(browser.find_element_by_class_name('td-wb-dropdown__item-content'))
select.select_by_index(1)
select.select_by_visible_text("Buy")

My error:

Select only works on <select> elements, not on <a>

Also, I'm able to click on a drop down itself. However, I'm just unsure how to select my option.

Tsymbol = browser.find_element_by_class_name('td-wb-dropdown__placeholder') # Clicking on BUY/SELL
Tsymbol.click()

2 Answers 2

3

Listen to your compiler, and trust it. It told you exactly why your code does not work. :-)

Don't use Select( part. Just find element by class name - use browser's method directly.

Another trick is to find elements, and check if any are present (returned list is not empty).

1
  • Thank you for your response. I understand that. I tried many options but i'm stuck. This drop down menu has 2 options "buy" and "sell". However, they have same basic class name "td-wb-dropdown__item-content"
    – shiny
    Oct 23, 2017 at 18:53
1

Got it solved, thank you

browser.find_element_by_xpath("//*[text()='Buy']").click();
3
  • XPath is flaky.Instead, get list of elements by class, loop over them, and check which one has the desired text, then click on that one. Oct 23, 2017 at 20:09
  • As an alternative to the suggestion made by @Peter Masiar, make your xpath more robust: browser.find_element_by_xpath("//a[@class='td-wb-dropdown__item-content' and text()='Buy']").click(); In the event that there were many elements on the page with the same class value, getting the full list and iterating through it will hurt performance. Using a more specific xpath with have a minimal effect on performance. Oct 23, 2017 at 21:34
  • 1
    @shiny, can you please expand this answer to explain why it solved your problem? It will help others in the future understand how you reached your conclusion
    – ECiurleo
    Oct 24, 2017 at 8:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.