I'm playing around with booking.com as I teach myself selenium / python. I'm trying to select a currency based on a known string e.g. 'USD'.
I'm able to click the currency icon to open the currency selection modal but then when I try to say find element where text = 'USD', I get:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
1. Here is the list item for 'USD':
<li class="ccff2b4c43 ea27cffb06"><button data-testid="selection-item" type="button" class="fc63351294 ea925ef36a bf97d4018a ae8177da1f cddb75f1fd"><div class="a1b3f50dcd a1f3ecff04 b2fe1a41c3 db7f07f643 d19ba76520"><div class="b1e6dd8416 aacd9d0b0a"><span class="cf67405157">U.S. Dollar<div class=" ea1163d21f">USD</div></span></div><div class=""></div></div></button></li>
2. Here is my known string I'm trying to search for:
currency = 'USD'
3. Here is my attempt at trying to find (then select) this string in the HTML:
selected_currency_element = self.find_element(By.CSS_SELECTOR, f'//span[text()={currency}]')
selected_currency_element.click()
I assume I don't need to read this modal's entire unordered list into an array then loop through it to find the string? Or do I?
Couple of things to rectify.
XPATH
and you have used CSS_SELECTOR
need to change to XPATH
selected_currency_element = self.find_element(By.XPATH, f'//span[text()={currency}]')
USD
is inside a DIV
tag of parent SPAN
So your xpath
should be like
selected_currency_element = self.find_element(By.XPATH, f'//span[.//div[text()="{currency}"]]')