英文:
find List Item based on known string
问题
以下是翻译的代码部分:
I'm playing around with [booking.com][1] as I teach myself selenium / python. I'm trying to select a currency based on a known string e.g. 'USD'.
我正在玩弄[booking.com][1],以便我可以自学selenium / python。我尝试基于已知字符串(例如'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:
我能够单击货币图标以打开货币选择模态窗口,但当我尝试查找文本为'USD'的元素时,出现以下错误:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
**1. Here is the list item for 'USD':**
**1. 这是'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:**
**2. 这是我试图搜索的已知字符串:**
currency = 'USD';
**3. Here is my attempt at trying to find (then select) this string in the HTML:**
**3. 这是我尝试在HTML中查找(然后选择)此字符串的尝试:**
selected_currency_element = self.find_element(By.CSS_SELECTOR, f'//span[text()={currency}]')
selected_currency_element.click()
我假设我不需要将此模态窗口的整个无序列表读入数组,然后遍历以查找该字符串?或者需要吗?
我假设我不需要将此模态窗口的整个无序列表读入数组,然后遍历以查找该字符串?或者需要吗?
[1]: https://www.booking.com/
[1]: https://www.booking.com/
英文:
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?
答案1
得分: 0
- 用于
XPATH
的语法已被使用,而您使用了CSS_SELECTOR
,需要更改为XPATH
。
selected_currency_element = self.find_element(By.XPATH, f'//span[text()={currency}]')
- 如果您看到节点
USD
在父级SPAN
的DIV
标签内,那么您的XPATH
应该如下:
selected_currency_element = self.find_element(By.XPATH, f'//span[.//div[text()="{currency}"]]')
英文:
Couple of things to rectify.
- The used syntax is for
XPATH
and you have usedCSS_SELECTOR
need to change toXPATH
selected_currency_element = self.find_element(By.XPATH, f'//span[text()={currency}]')
- If you see the node
USD
is inside aDIV
tag of parentSPAN
So yourxpath
should be like
selected_currency_element = self.find_element(By.XPATH, f'//span[.//div[text()="{currency}"]]')
答案2
得分: 0
Instead of using the abbreviation, I used the long name. This works pretty good. By the way, I use Firefox as my browser
from selenium.webdriver.common.by import By
from selenium import webdriver
def change_currency(country):
dvr = webdriver.Firefox()
dvr.get("https://www.booking.com/")
dvr.find_elements(By.XPATH, "//span[contains(text(),'USD')])[0].click()
dvr.find_elements(By.XPATH, f"//span[contains(text(),'{country}')])[0].click()
change_currency("Thai Baht")
英文:
Instead of using the abbreviation, I used the long name. This works pretty good. By the way, I use Firefox as my browser
from selenium.webdriver.common.by import By
from selenium import webdriver
def change_currenecy(country):
dvr = webdriver.Firefox()
dvr.get("https://www.booking.com/")
dvr.find_elements(By.XPATH, "//span[contains(text(),'USD')]")[0].click()
dvr.find_elements(By.XPATH, f"//span[contains(text(),'{country}')]")[0].click()
change_currenecy("Thai Baht")
答案3
得分: 0
selected_currency_element = self.find_element(By.XPATH, f'xpath_value')
- 所需的文本 USD 位于其父元素
<span>
的子元素<div>
中,所以你需要再深入一步。
解决方案
你有效的代码行将是:
currency = 'USD'
selected_currency_element = self.find_element(By.XPATH, f'//span//div[text()={currency}]')
selected_currency_element.click()
英文:
You need to take care of a couple of things here:
-
Though you mentioed
By.CSS_SELECTOR
but the value is of a xpath. So you need to change the locator strategy as:selected_currency_element = self.find_element(By.XPATH, f'xpath_value')
-
The desired text USD is within the descendant
<div>
of it's parent<span>
, so you need to go a step deeper.
Solution
Your effective line of code will be:
currency = 'USD'
selected_currency_element = self.find_element(By.XPATH, f'//span//div[text()={currency}]')
selected_currency_element.click()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论