简体   繁体   English

从 Selenium (Python) 中的给定选项列表中单击正确的选项

[英]Clicking on the correct option from a given list of options in Selenium (Python)

在此处输入图像描述

I am trying to give input to the 'Currency I have' text box.我正在尝试向“我拥有的货币”文本框提供输入。 This input is in the variable 'currency'.此输入在变量“货币”中。 After I give the input in the text box, it displays a number of options.在我在文本框中输入后,它会显示许多选项。 I want to do a case insensitive match on the 3 letter currency code I enter and the options the text box displays in the drop-down, and select the correct one.我想对我输入的 3 个字母的货币代码和下拉菜单中显示的选项进行不区分大小写的匹配,select 是正确的。 The page I am testing: https://www.oanda.com/fx-for-business/historical-rates我正在测试的页面: https://www.oanda.com/fx-for-business/historical-rates

currency = currency_list.loc[i,'currency']
    print(f'\nFETCHING DATA FOR : {currency}')
    df=pd.DataFrame()
    
    #input our currency in "currency I have" text-box
    WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#havePicker > div'))).click()
    currency_have = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.CSS_SELECTOR,'#havePicker > div > input')))
    
    try:
        currency_have.clear() 
    except:
        pass
    
    currency_have.send_keys(currency)
    options = WebDriverWait(driver, 2).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'#havePicker > div > ul li')))
    
    div_tags = [li_tags.find_elements_by_tag_name('div') for li_tags in options]
        
    for div_tag in div_tags:
        test = div_tag[1]
        if (test.text.casefold()) == (currency.casefold()):
            return test
        else:
            continue

the return statement in my code is incorrect.我的代码中的 return 语句不正确。 How do I proceed further to achieve my goal?我如何进一步实现我的目标?

Please, any help would be appreciated.请,任何帮助将不胜感激。 I'm new to selenium.我是 selenium 的新手。

In order to wait for dropdown result, the order of your actions should be the following:为了等待下拉结果,您的操作顺序应如下所示:

  • Clear "Currency I have" dropdown清除“我拥有的货币”下拉菜单
  • Click it (not completely sure if this action is required, test it)单击它(不完全确定是否需要此操作,测试它)
  • Wait for all values等待所有值
  • Send value ("Australian dollar") # not AUD as there is also Saudi Riyal in search results发送价值(“澳元”)#不是澳元,因为搜索结果中也有沙特里亚尔

This part of code should look like below:这部分代码应如下所示:

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#havePicker > div'))).click()
currency_have = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, '#havePicker > div > input')))

currency_have.clear()
currency_have.click()

options = WebDriverWait(driver, 10).until(
    EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#havePicker > div > ul li')))
currency_have.send_keys("Australian dollar")

Please note that you have two results on AUD .请注意,您在AUD上有两个结果。 That's why I used the full currency name.这就是我使用完整货币名称的原因。 If you want to use just aud, use split() Check here how to use it How to extract the substring between two markers?如果您只想使用 aud,请使用split()在这里查看如何使用它如何在两个标记之间提取 substring? Then improve your for loop.然后改进你的 for 循环。

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

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