简体   繁体   English

如何使用 python 从 selenium 的下拉列表中 select 项目?

[英]How do I select an item from dropdown in selenium using python?

在此处输入图像描述

I'm trying to select 'Newest' from the drop-down menu.我正在尝试从下拉菜单中选择 select 'Newest'。

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
# options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options)

url = 'https://play.google.com/store/apps/details?id=com.whatsapp&hl=en&showAllReviews=true'
driver.get(url)

state_selection = driver.find_element_by_xpath("//div[.='%s']" % "Most relevant")
state_selection.click()
state_selection.send_keys(Keys.UP)
state_selection.send_keys(Keys.UP)
state_selection2 = driver.find_element_by_xpath("//div[.='%s']" % "Newest")
state_selection2.send_keys(Keys.RETURN)

but as soon as it reaches Newest and as I send command to press enter(as shown in code),it resets to "Most Relevent".但是一旦它到达最新并且我发送命令按回车(如代码所示),它就会重置为“最相关”。 I'm not able to get my head around on how to achieve this.我无法弄清楚如何实现这一目标。

After you have clicked state_selection, something like this will click "Newest":单击 state_selection 后,类似这样的内容将单击“最新”:

driver.find_element_by_xpath("//div[@role='option']/span[contains(text(),'Newest')]").click()

The more robust method would be working with WebdriverWait to allow the DOM to update, so:更强大的方法是使用WebdriverWait以允许 DOM 更新,因此:

WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH, "//div[@role='option']/span[contains(text(),'Newest')]"))).click()

Note you need these imports for WebdriverWait :请注意,您需要这些导入WebdriverWait

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

There are different ways to find有不同的方法可以找到

  • Index指数
  • Value价值
  • Visible Text可见文本

When you use the xpath if the values are changed in future,it pick that element present in that location only.So its better to user select by visible text当您使用 xpath 时,如果将来更改值,它只会选择该位置中存在的元素。因此,通过可见文本对用户 select 更好

state_selection=Select(driver.find_element_by_xpath("//div[.='%s']" % "Most relevant").click();
state_selection.select_by_visible_text("Dropdown Visible Text")

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

相关问题 如何在 python 中使用 selenium select 下拉列表中的一个项目 - how to select an item of a dropdown using selenium in python Selenium Python:如何从下拉菜单中选择项目 - Selenium Python: How to select item from a dropdown selenium/python 我不能 select 下拉列表中的项目 - selenium/python i cant select a item in dropdown How can I select item from dropdown, when option's element not interactable via selenium/python? - How can I select item from dropdown, when option's element not interactable via selenium/python? 如何使用 Xpath 为机器人框架 select 下拉列表中的列表项 Selenium Python - How to select a list item from the dropdown list using Xpath for Robot framework Selenium Python 如何使用 Selenium 在下拉列表中的 select 项目? - How to select item in dropdown list using Selenium? 如何在Python上使用Selenium / Pyautogui从下拉列表中选择“全部” - How to select 'All' from dropdown using Selenium/Pyautogui on Python 如何在 python 中使用 selenium 从下拉列表中选择 select - How to select an option from dropdown using selenium in python 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from the dropdown menu using Selenium and Python 使用Selenium和Python从下拉菜单中选择 - Select from dropdown using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM