我尝试使用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()
拥有真正的html会有所帮助,但您可以尝试使用一个多值类,甚至可以颠倒类的顺序。示例(在html示例上进行测试时,这两种方法都有效):
或者
由于下拉列表基于
<span>
和<div>
节点,因此您不能使用Select
类并单击website中的选项Popular,您必须为element_to_be_clickable()
导入WebDriverWait,并且您可以使用以下Locator Strategies:使用
XPATH
:浏览器快照:
相关问题 更多 >
编程相关推荐