如何通过 Python 中的部分文本 select - selenium
how to select by a partial text in Python - selenium
如何通过名称的一部分 select 下拉元素?
我想 select 一个基于数据库值的选项,但是这个值没有下拉元素的完整名称,有什么办法可以让 selenium 以我的数据库值作为部分文本来查找选项?
modelo = googleSheet.modelo.upper().strip()
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div/div[1]/form/fieldset[6]/div/ul/fieldset[3]/div/ul/fieldset[3]/div/ul/fieldset/div/ul/li/label'))))
select.select_by_visible_text(modelo)
我想要 select 的下拉选项是“Terrano II 2.7 xpto ol”,但我的数据库值只是 Terrano II 2.7
感谢您的帮助
如果您首先提取下拉文本内容,然后检查您的数据库查询是否在文本中呢?像这样:
driver.select_by_visible_text()
已经 strip()
。你不需要它。
另外,从这个方法定义:
Select all options that display text matching the argument. That is, when given "Bar" this would select an option like:
<option value="foo">Bar</option>
:Args:
- text - The visible text to match against
因此,您需要准确预期可见的选项。
代码中的另一个问题是传递变量的方式。
dropdown_option = "Some text you expect to see in the dropdown"
locator = driver.find_element_by_id("id") # or any other locator
select = Select(locator)
if locator is not None:
for option in select.options:
select.select_by_visible_text(dropdown_option)
此实现使调试更容易。例如,您可以在选择所需选项之前打印下拉列表中的所有值。
如果打开下拉菜单需要很长时间,或者其他元素使您的下拉菜单暂时不可见,请在选择前添加一个单独的等待时间。
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located(
(By.CSS_SELECTOR, "Unique css selector of the first option in dropdown")))
如何通过名称的一部分 select 下拉元素? 我想 select 一个基于数据库值的选项,但是这个值没有下拉元素的完整名称,有什么办法可以让 selenium 以我的数据库值作为部分文本来查找选项?
modelo = googleSheet.modelo.upper().strip()
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div/div[1]/form/fieldset[6]/div/ul/fieldset[3]/div/ul/fieldset[3]/div/ul/fieldset/div/ul/li/label'))))
select.select_by_visible_text(modelo)
我想要 select 的下拉选项是“Terrano II 2.7 xpto ol”,但我的数据库值只是 Terrano II 2.7
感谢您的帮助
如果您首先提取下拉文本内容,然后检查您的数据库查询是否在文本中呢?像这样:
driver.select_by_visible_text()
已经 strip()
。你不需要它。
另外,从这个方法定义:
Select all options that display text matching the argument. That is, when given "Bar" this would select an option like:
<option value="foo">Bar</option>
:Args:
- text - The visible text to match against
因此,您需要准确预期可见的选项。 代码中的另一个问题是传递变量的方式。
dropdown_option = "Some text you expect to see in the dropdown"
locator = driver.find_element_by_id("id") # or any other locator
select = Select(locator)
if locator is not None:
for option in select.options:
select.select_by_visible_text(dropdown_option)
此实现使调试更容易。例如,您可以在选择所需选项之前打印下拉列表中的所有值。
如果打开下拉菜单需要很长时间,或者其他元素使您的下拉菜单暂时不可见,请在选择前添加一个单独的等待时间。
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located(
(By.CSS_SELECTOR, "Unique css selector of the first option in dropdown")))