如何通过下拉列表元素名称的一部分来选择该元素? 我想根据DB值选择一个选项,但该值没有下拉元素的完整名称,有没有办法让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)
我想选择的下拉选项是“Terrano II 2.7 xpto ol”,但我的数据库值只是Terrano II 2.7
谢谢你的帮助
如果先提取下拉文本内容,然后检查db查询是否为文本,会怎么样?大概是这样的:
Selenium Select - Selecting dropdown option by part of the text
driver.select_by_visible_text()
已经做了strip()
。你不需要它。 此外,根据此方法定义:因此,您需要准确地预期可见的选项。 代码中的另一个问题是传递变量的方式
此实现使调试更容易。例如,在选择所需选项之前,可以打印下拉列表中的所有值
如果下拉列表打开需要花费大量时间,或者其他元素使下拉列表暂时不可见,请在选择之前添加单独的等待
相关问题 更多 >
编程相关推荐