需要XPath来选择下拉菜单中的第一个/随机元素

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

Need Xpath to select the first/random element from the dropdown

问题

我需要一个XPath表达式来读取所有的值,我有一个名为“切片偏好”的下拉菜单,其值始终设置为“标准厚度”,我需要从下拉菜单中选择不同的值。下面是下拉菜单中的值。

“刨”,“切薄”,“标准厚度”,“切厚”

这是XPath表达式:“//div//span[contains(@class, 'text-gray-900')]”,但它不能获取下拉菜单中的所有值。

问题是,当单击下拉菜单时,开发工具中会出现一个定位器,但当我单击定位器时,它会消失(截图中的#2)。

有人可以帮忙获取XPath表达式吗?

需要XPath来选择下拉菜单中的第一个/随机元素

这是网站

这是在开发工具中看到的定位器详细信息。

英文:

I need an xpath expression to read all the values, I have a dropdown called "Slicing Preference" whose value is always set to "Standard Thickness", and I need to select a different value from the dropdown. These are the values in the dropdown.

"Shaved", "Sliced Thin", "Standard Thickness", "Sliced Thick"

This is the XPath "//div//span[contains(@class, 'text-gray-900')]", and it does not get me all the values from the dropdown.

The problem is, when the dropdown is clicked there is a locator that shows up in dev tools, but when I click on the locator it disappears (#2 in the screenshot)

Can someone help on getting the XPath expression?

需要XPath来选择下拉菜单中的第一个/随机元素

This is the website

This is the locator details seen in the dev tools.

<div class="flex flex-col cursor-default" xpath="1">
    <label class="font-bold leading-none text-sm" for="_fzeis58un" id="_fzeis58un_label"><span>Slicing Preference </span><span aria-hidden="true" class="text-red-500">*</span><span class="sr-only">This field is required</span></label>
    <div class="relative">
        <div aria-role="listbox" aria-required="true" aria-labelledby="_fzeis58un_label" tabindex="0" id="_fzeis58un" class="text-lg py-2 leading-tight border-b border-gray-600 select-none">
            <div class="text-gray-800 flex flex-row items-center justify-between">
                <span class="text-gray-900">Standard Thickness</span>
                <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-down" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="text-sm text-black svg-inline--fa fa-chevron-down fa-w-14">
                    <path fill="currentColor" d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z" class=""></path>
                </svg>
            </div>
            <!---->
        </div>
    </div>
    <!----><!---->
</div>

答案1

得分: 0

以下是代码部分的翻译:

这是如何实现的,

import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])

driver = webdriver.Chrome(options=options)
driver.get("https://deliorder-web.shoprite.com/stores/279/departments/553/products/258234")
time.sleep(5)

driver.execute_script("window.scrollBy(0, 300);")
driver.find_element(By.XPATH, '//span[contains(text(), "Standard Thickness")]').click()
time.sleep(2)

slicing_preference = ["Shaved", "Sliced Thin", "Standard Thickness", "Sliced Thick"]
# choose Sliced Thin (slicing_preference[1] is "Sliced Thin")
driver.find_element(By.XPATH, f'//span[contains(text(), "{slicing_preference[1]}")]').click()
time.sleep(2)

I hope it solves your problem.

希望能解决您的问题。

英文:

This is how you can do it,

import time
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])

driver = webdriver.Chrome(options=options)
driver.get("https://deliorder-web.shoprite.com/stores/279/departments/553/products/258234")
time.sleep(5)

driver.execute_script("window.scrollBy(0, 300);")
driver.find_element(By.XPATH, '//span[contains(text(), "Standard Thickness")]').click()
time.sleep(2)

slicing_preference = ["Shaved", "Sliced Thin", "Standard Thickness", "Sliced Thick"]
# choose Sliced Thin (slicing_preference[1] is "Sliced Thin")
driver.find_element(By.XPATH, f'//span[contains(text(), "{slicing_preference[1]}")]').click()
time.sleep(2)

I hope it solves your problem.

huangapple
  • 本文由 发表于 2023年4月7日 05:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953970.html
匿名

发表评论

匿名网友

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

确定