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

2024-05-19 03:02:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用selenium处理一个下拉菜单来单击这个website中的“Popular”选项,但是没有一个我发现的例子不适合这个。你知道吗

<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()

Tags: tobyvaluedriverelementselectselectorxpath
2条回答

拥有真正的html会有所帮助,但您可以尝试使用一个多值类,甚至可以颠倒类的顺序。示例(在html示例上进行测试时,这两种方法都有效):

.explorerSortMenu__explorerSortPopoutMenu 3pMwT.select__select 2gOcq

或者

.select__select 2gOcq

由于下拉列表基于<span><div>节点,因此您不能使用Select并单击website中的选项Popular,您必须为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

相关问题 更多 >

    热门问题