简体   繁体   English

如何在 selenium 和 python 中使用下拉菜单和输入文本框?

[英]How to work with drop down menu and input text boxes in selenium and python?

I'm working with selenium in python and in this web page I want to choose between different options in a drop down menu and then enter some values.我正在 python 中使用 selenium,在此网页中,我想在下拉菜单中的不同选项之间进行选择,然后输入一些值。 In that webpage, after I click on the "new order" button, a new windows pops up and I have to choose between symbols, for example, "USDCAD".在该网页中,单击“新订单”按钮后,会弹出一个新窗口,我必须在符号之间进行选择,例如“USDCAD”。 After that I have to enter some values such as "take profit" and "stop loss".之后,我必须输入一些值,例如“获利”和“止损”。 I don't know how to do these because unfortunately I don't know much about how web pages work and what HTML is and...!我不知道如何做这些,因为不幸的是我不太了解网页的工作原理以及 HTML 是什么和......! This is the code that I wrote for selecting between drop down menu options but I get error:这是我为在下拉菜单选项之间进行选择而编写的代码,但出现错误:

def fast_multiselect(driver, element_id, labels):
    select = Select(driver.find_element_by_id(element_id))
    for label in labels:
        select.select_by_visible_text(label)
fast_multiselect(driver, 'symbol', "USDCAD")

And this is the error that I get:这是我得到的错误:

selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on <th>

Complete code:完整代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.mql5.com/en/trading')
time.sleep(10)
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='webTerminalHost']")))
time.sleep(10)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='New Order']/span"))).click()
time.sleep(5)
def fast_multiselect(driver, element_id, labels):
    select = Select(driver.find_element_by_id(element_id))
    for label in labels:
        select.select_by_visible_text(label)
fast_multiselect(driver, 'symbol', "USDCAD")

I will be so thankful if you help me choose from the drop down menu and enter those values.如果您帮我从下拉菜单中选择并输入这些值,我将不胜感激。 And also if you explain some important and basic things or if a quick guide (maybe a useful link) which helps me understand how I should deal with HTML codes for basic jobs like my question.而且,如果你解释了一些重要和基本的事情,或者如果一个快速指南(可能是一个有用的链接)帮助我理解我应该如何处理像我的问题这样的基本工作的 HTML 代码。

You need to target a select tag.您需要定位一个选择标签。

<select class="input-combobox" id="order-dialog-symbol">
<option value="USDCAD">USDCAD, US Dollar vs Canadian Dollar</option>

The bottom would work底部会起作用

fast_multiselect(driver, 'order-dialog-symbol', ["USDCAD"])

If you replaced如果你更换了

select.select_by_visible_text(label)

with

select.select_by_value(label)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM