简体   繁体   English

如何在Python中使用Selenium从https://www.amazon.in/的下拉列表框中选择选项Books?

[英]How to select the option Books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python?

How to select the option books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python? 如何在Python中使用Selenium从https://www.amazon.in/的下拉列表框中选择选项书?

I am trying the code: 我正在尝试代码:

driver.find_element_by_xpath("//*[@id='searchDropdownBox']").send_keys('Books')

在此处输入图片说明

Try this: 尝试这个:

select = Select(driver.find_element_by_id('searchDropdownBox'))

# select by visible text
select.select_by_visible_text('Books')

The drop down list for the categories is within a <select> tag so ideally you need to use the select class inducing WebDriverWait for the desired element to be clickable and you can use the following solution: 类别下拉列表位于<select>标记内,因此,理想情况下,您需要使用诱发WebDriverWaitselect类来使所需元素可单击,并可以使用以下解决方案:

  • Code Block: 代码块:

     from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument("--disable-extensions") driver = webdriver.Chrome(options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get('https://www.amazon.in/') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='nav-search-label']"))) mySelect = Select(driver.find_element_by_xpath("//select[@id='searchDropdownBox']")) mySelect.select_by_visible_text('Books') print((mySelect.first_selected_option).text) 
  • Console Output: 控制台输出:

     Books 
  • Browser Snapshot: 浏览器快照:

amazon_books

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SELECT 使用 Selenium 的下拉选项 Python - SELECT option from dropdown using Selenium Python 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from the dropdown menu using Selenium and Python 如何在 python 中使用 selenium 从下拉列表中选择 select - How to select an option from dropdown using selenium in python 当它不是选择元素时如何从列表框中选择一个选项? (硒,Python) - How to select an option from a listbox when it is not a select element? (Selenium, Python) 如何使用 Selenium 和 ZA7F117F35426B9327 在复杂网站中找到下拉 select 元素和 select 选项 - How to locate the dropdown select element and select an option within a complex website using Selenium and Python How to select a country from https://www.aliexpress.com/ ship to drowdown menu using Selenium and Python - How to select a country from https://www.aliexpress.com/ ship to drowdown menu using Selenium and Python Python 和 Selenium || 如何 select 列表框中的第一个选项? - Python with Selenium || how to select first option in listbox? 使用Selenium(python)从条件下拉列表中选择一个选项 - Using selenium (python) to select an option from a conditional dropdown 使用 selenium 和 python 从下拉按钮中选择选项 - Select option from a dropdown button using selenium with python 使用 python selenium 到 select 来自谷歌表单下拉列表的选项 - Using python selenium to select a option from a google form dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM