简体   繁体   English

SELECT 使用 Selenium 的下拉选项 Python

[英]SELECT option from dropdown using Selenium Python

been dealing with a countries dropdown using Selenium Python;一直在使用 Selenium Python 处理国家/地区下拉列表;

here is the Div tag of the drop-down menu:这是下拉菜单的 Div 标签:

<div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c95-29"><div class="mat-select-value ng-tns-c95-29" id="mat-select-value-1"><span class="mat-select-placeholder mat-select-min-line ng-tns-c95-29 ng-star-inserted"></span><!----><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-29"><div class="mat-select-arrow ng-tns-c95-29"></div></div></div><!---->

and here is the Div tag for the targeted option:这是目标选项的 Div 标签:

<div class="mat-select-value ng-tns-c95-29" id="mat-select-value-1"><!----><span class="mat-select-value-text ng-tns-c95-29 ng-star-inserted" style=""><span class="mat-select-min-line ng-tns-c95-29 ng-star-inserted">MAROKO</span><!----><!----></span><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-29"><div class="mat-select-arrow ng-tns-c95-29"></div></div>

and here's the specific Span Tag for the targeted option这是目标选项的特定跨度标签

<span class="mat-select-min-line ng-tns-c95-29 ng-star-inserted">MAROKO</span><!----><!---->

the code used to select an option from that dropdown is:用于 select 的代码是:

Country=browser.find_element(By.ID, 'mat-select-value-1')
time.sleep(5) 
drop=Select(Country) 
time.sleep(5) 
drop.select_by_visible_text("MAROKO")

output output

Exception has occurred: UnexpectedTagNameException
Message: Select only works on <select> elements, not on <div>
  File "C:\Users\test\form.py", line 31, in <module>
    drop=Select(Country)

I went with drop.select_by_visible_text since I believe it's the only available option!我选择了drop.select_by_visible_text因为我相信这是唯一可用的选项!

Imports used使用进口

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

I would appreciate any helpful comment.我将不胜感激任何有用的评论。

Cheers干杯

Tried this specifcly this;特地尝试了这个;

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id="mat-radio-2"]/label/span[1]/span[1][text()='MAROKO']"))).click()

but it seems that there's a syntax error但似乎有语法错误

You can use the selenium's Select() class only for html <select> tags.您只能将 selenium 的Select() class 用于 html <select>标签。 But this dropdown is implemented with <div> , so you can handle it just like html elements.但是这个下拉列表是用<div>实现的,所以你可以像处理 html 元素一样处理它。
First you need to click the dropdown to expand the options and then click the option you need:首先,您需要单击下拉菜单以展开选项,然后单击您需要的选项:

WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="mat-select-trigger ng-tns-c95-29"]'))).click()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'mat-select-value-1'))).click()

I've created the locators using the html parts you provided.我使用您提供的 html 部件创建了定位器。 Perharps you will need to update them.也许您需要更新它们。

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

相关问题 使用Selenium(python)从条件下拉列表中选择一个选项 - Using selenium (python) to select an option from a conditional dropdown 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from the dropdown menu using Selenium and Python 使用 selenium 和 python 从下拉按钮中选择选项 - Select option from a dropdown button using selenium with python 如何在 python 中使用 selenium 从下拉列表中选择 select - How to select an option from dropdown using selenium in python 使用 python selenium 到 select 来自谷歌表单下拉列表的选项 - Using python selenium to select a option from a google form dropdown 使用 Selenium 和 Python 从下拉列表中选择一个选项 - Selecting an option from a dropdown using Selenium and Python 从下拉框中选择而不使用 Select 或 Option 标签:selenium python - Select from a dropdown box without using the Select or Option tags: selenium python 如何使用 Selenium 和 Python 从 GoogleForm 非选择下拉列表中选择一个选项 - How to select an option from the GoogleForm non select dropdown using Selenium and Python 使用Selenium和Python从下拉菜单中选择 - Select from dropdown using Selenium and Python 如何在Python中使用Selenium从https://www.amazon.in/的下拉列表框中选择选项Books? - How to select the option Books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM