sprogissd
sprogissd

Reputation: 3075

How to get labels of elements by their IDs with Selenium Webdriver with Python?

I have drop-downs that have assigned IDs and each of them has a name visible in the UI. Below is an example. The ID of the drop-down is Filter_Comparison, its label in the UI is 'Primary Comparison', and the label is described as <label for="Filter_Comparison">Primary Comparison</label>. How can I grab the text of that label by the ID that it is assigned to?

enter image description here

enter image description here

Sure enough the following is not going to work as it is going to give the text of the option that is currently selected in the drop-down (in this case it is 'Benchmark Focused'):

browser.find_element_by_id(`Filter_Comparison`).text

Upvotes: 0

Views: 810

Answers (1)

Sers
Sers

Reputation: 12255

Attribute for in the label already is id of dropdown, you can use it.

filterComparisonId = 'Filter_Comparison'
filterComparison = browser.find_element_by_css_selector('label[for="'+ filterComparisonId +'"]').text
filterComparisonElement = browser.find_element_by_id(filterComparisonId)

Upvotes: 1

Related Questions