gpt4 book ai didi

python - 如何使用动态类值处理 Selenium 中的非选择下拉菜单

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:53 24 4
gpt4 key购买 nike

我尝试处理下拉菜单以单击此 website 中的“热门”选项使用 selenium,但我发现没有一个示例不适合这一点。

    <select class="select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT">
<option value="desc__" selected="">Highest User Rating</option><option
value="desc__discount_percent">Discount</option><option value="asc__price">Price: Low to High</option><option value="desc__price">Price: High to Low</option><option value="desc__ratings_count">Popular</option></select>

使用过CSS、Xpath和Select,但结果都是一样的:没有这样的元素。您可以在下面看到尝试和输出。

有什么想法我做错了吗?

CSS 选择器

browser.find_element_by_css_selector('.select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT')

Message: no such element: Unable to locate element: {"method":"css selector","selector":".select__select--2gOcq.explorerSortMenu__explorerSortPopoutMenu--3pMwT"}

Xpath

browser.find_element_by_xpath('//input[starts-with(@class,"select__select--2gOcq")]')

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[starts-with(@class,"select__select--2gOcq")]"}

选择

Select(browser.find_element_by_xpath("//*[@class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"))

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='select__select--2gOcq explorerSortMenu__explorerSortPopoutMenu--3pMwT']"}

更新:

执行下面的代码后,元素已成功定位,但是我捕获了TimeoutException

driver = webdriver.Chrome()
driver.get(URL)
try:
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@class, 'select__select--') and contains(@class, 'explorerSortMenu__explorerSortPopoutMenu--')]"))))
select.select_by_visible_text('Popular')
select.click()
finally:
driver.quit()

最佳答案

由于下拉列表基于<span><div>节点,因此您不能使用 Select class 并单击 website 中的流行选项您必须为element_to_be_clickable()引发WebDriverWait您可以使用以下任一 Locator Strategies :

  • 使用 XPATH :

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://www.vivino.com/explore?e=eJzLLbI11jNVy83MszU1MFDLTaywNTIAMpIrbT391JKBRJBaga2hWnqabVliUWZqSWKOWm6yrVp-EhDbpqQWJ6uVl0THAlWAKSMAxOAYsg==")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@class, 'responsiveDropdownMenu__title--')]//following::span[starts-with(@class, 'responsiveDropdownMenu__label--')]"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@class, 'responsiveDropdownMenu__menu--')]//a[@id='desc__ratings_count']"))).click()
  • 浏览器快照:

vivino

关于python - 如何使用动态类值处理 Selenium 中的非选择下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com