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

问题描述 投票:0回答:2

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

我正在尝试代码:

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

enter image description here

python selenium amazon html-select webdriverwait
2个回答
2
投票

试试这个:

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

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

0
投票

类别的下拉列表在<select>标记内,因此理想情况下,您需要使用select类来引导WebDriverWait以使所需元素可单击,您可以使用以下解决方案:

  • 代码块: 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)
  • 控制台输出: Books
  • 浏览器快照:

amazon_books

© www.soinside.com 2019 - 2024. All rights reserved.