如何使用动态 class 值处理 selenium 中的非 select 下拉菜单
How to handle a non select dropdown menu in selenium with dynamic class value
我尝试使用 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 Select或
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
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()
拥有实际的 html 会有所帮助,但您可以尝试使用多值 class 之一,甚至可以颠倒 class 顺序。示例(这些都在 html 样本上测试时有效):
.explorerSortMenu__explorerSortPopoutMenu--3pMwT.select__select--2gOcq
或
.select__select--2gOcq
由于 下拉列表 基于 <span>
和 <div>
节点,因此您不能使用 Select
class 并在 website you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following :
中单击 Popular 选项
使用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()
浏览器快照:
我尝试使用 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 Select或
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
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()
拥有实际的 html 会有所帮助,但您可以尝试使用多值 class 之一,甚至可以颠倒 class 顺序。示例(这些都在 html 样本上测试时有效):
.explorerSortMenu__explorerSortPopoutMenu--3pMwT.select__select--2gOcq
或
.select__select--2gOcq
由于 下拉列表 基于 <span>
和 <div>
节点,因此您不能使用 Select
class 并在 website you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following
使用
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()
浏览器快照: