如何使用Python中的Selenium从https://www.amazon.in/中的下拉列表框中选择选项书?
我正在尝试代码:
driver.find_element_by_xpath("//*[@id='searchDropdownBox']").send_keys('Books')
试试这个:
select = Select(driver.find_element_by_id('searchDropdownBox'))
# select by visible text
select.select_by_visible_text('Books')
类别的下拉列表在<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