[英]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>
标记内,因此,理想情况下,您需要使用诱发WebDriverWait的select类来使所需元素可单击,并可以使用以下解决方案:
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: 浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.