Reputation: 773
I am trying to follow this tutorial enter link description here he is trying to show us how to handle multi-select dropdown.
I am confused about why I don't get the expected drop-list item text provided on the drop-down. here is my code. I am targetting only the spans present inside div#comboTree950771DropDownContainer. I get nothing as output
drop_list = driver.find_elements(By.CSS_SELECTOR , 'div#comboTree950771DropDownContainer span.comboTreeItemTitle')
for item in drop_list:
print(item.text)
but below code works fine according to his solution, I am wondering why the above code does not work which is targetting more accurate tags and elements.
drop_list = driver.find_elements(By.CSS_SELECTOR , 'span.comboTreeItemTitle')
for item in drop_list:
print(item.text)
here is the output for the second code with lots of space at the end.
choice 1
choice 2
choice 2 1
choice 2 2
choice 2 3
choice 3
choice 4
choice 5
choice 6
choice 6 1
choice 6 2
choice 6 2 1
choice 6 2 2
choice 6 2 3
choice 7
this the link to the website: enter link description here
Upvotes: 0
Views: 35
Reputation: 471
It seems the IDs for the dropdown container divs are dynamically generated everytime you request the page.
So one time the ID is comboTree950771DropDownContainer
the next time it will be something like comboTree123456DropDownContainer
notice the numbers changing. This is used to scope CSS (it has the added effect of thwarting basic scrapers).
You can check it yourself by taking note of the ID for the container then refreshing the page and rechecking.
One way around this is to use another selector such as the class, or the relation of the item compared another one, also you could read about XPath here, here. and here.
Upvotes: 1