基于已知字符串查找列表项

huangapple go评论71阅读模式
英文:

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':

&lt;li class=&quot;ccff2b4c43 ea27cffb06&quot;&gt;&lt;button data-testid=&quot;selection-item&quot; type=&quot;button&quot; class=&quot;fc63351294 ea925ef36a bf97d4018a ae8177da1f cddb75f1fd&quot;&gt;&lt;div class=&quot;a1b3f50dcd a1f3ecff04 b2fe1a41c3 db7f07f643 d19ba76520&quot;&gt;&lt;div class=&quot;b1e6dd8416 aacd9d0b0a&quot;&gt;&lt;span class=&quot;cf67405157&quot;&gt;U.S. Dollar&lt;div class=&quot; ea1163d21f&quot;&gt;USD&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/button&gt;&lt;/li&gt;

2. Here is my known string I'm trying to search for:

currency = &#39;USD&#39;

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&#39;//span[text()={currency}]&#39;)
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

  1. 用于 XPATH 的语法已被使用,而您使用了 CSS_SELECTOR,需要更改为 XPATH

selected_currency_element = self.find_element(By.XPATH, f'//span[text()={currency}]')

  1. 如果您看到节点 USD 在父级 SPANDIV 标签内,那么您的 XPATH 应该如下:

selected_currency_element = self.find_element(By.XPATH, f'//span[.//div[text()="{currency}"]]')

英文:

Couple of things to rectify.

  1. The used syntax is for XPATH and you have used CSS_SELECTOR need to change to XPATH

selected_currency_element = self.find_element(By.XPATH, f&#39;//span[text()={currency}]&#39;)

  1. If you see the node USD is inside a DIV tag of parent SPAN
    So your xpath should be like
    selected_currency_element = self.find_element(By.XPATH, f&#39;//span[.//div[text()=&quot;{currency}&quot;]]&#39;)

答案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(&quot;https://www.booking.com/&quot;)
dvr.find_elements(By.XPATH, &quot;//span[contains(text(),&#39;USD&#39;)]&quot;)[0].click()
dvr.find_elements(By.XPATH, f&quot;//span[contains(text(),&#39;{country}&#39;)]&quot;)[0].click()
change_currenecy(&quot;Thai Baht&quot;)

答案3

得分: 0

  • 虽然你提到了By.CSS_SELECTOR,但值是一个xpath。所以你需要更改定位策略如下:
selected_currency_element = self.find_element(By.XPATH, f'xpath_value')
  • 所需的文本 USD 位于其父元素&lt;span&gt;的子元素&lt;div&gt;中,所以你需要再深入一步。

解决方案

你有效的代码行将是:

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&#39;xpath_value&#39;)
    
  • The desired text USD is within the descendant &lt;div&gt; of it's parent &lt;span&gt;, so you need to go a step deeper.


Solution

Your effective line of code will be:

currency = &#39;USD&#39;
selected_currency_element = self.find_element(By.XPATH, f&#39;//span//div[text()={currency}]&#39;)
selected_currency_element.click()

huangapple
  • 本文由 发表于 2023年2月18日 00:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定