Selenium 下拉菜单:有效定位器在哪里?

Selenium Drop Down Menu : where is the valid locator?

我的问题如下:

我想从下拉菜单中select。

html 给出了这个:

<select class="js-order-type-buysell order-buysell-selector" style="opacity: 0" data-width="100%" tabindex="null">
                                            <option selected="selected">Bitte wählen...</option>
                                            <option value="buy">Kauf</option>
                                            <option value="sell">Verkauf</option>
                                        </select>

这也嵌入在 div

<div class="dropdown bootstrap-select js-order-type-buysell order-buysell-selector bs3" style="width: 100%;">

我的代码如下

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.common.exceptions import NoSuchElementException

username = "*******"
password = "*******"

url = "https://www.wikifolio.com/dynamic/de/de/login/login?ReturnUrl=/de/de/home&_=1632037782306"


driver = webdriver.Chrome(executable_path=r'\Users\Benjamin\Downloads\chromedriver_win32\chromedriver.exe')
wait = WebDriverWait(driver, 20)

driver.get(url)
driver.find_element_by_name("Username").send_keys(username)
driver.find_element_by_name("Password").send_keys(password)

driver.find_element_by_css_selector("button").click()

driver.get("https://www.wikifolio.com/de/de/meine-wikifolios/trade/wf00wiking")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".c-disclaimer .js-disclaimer__abort, .c-disclaimer .js-disclaimer__change"))).click()



wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@data-description='AKER CARB.CAPT.AS NK1']"))).click()

e = driver.find_element_by_xpath('//*[@id="trading-modal-root"]')
Select(e).select_by_value('selected').click()

所以我特别要求最后一部分的解决方案:

e = driver.find_element_by_xpath('//*[@id="trading-modal-root"]')
Select(e).select_by_value('selected').click()

有没有办法做到这一点,鉴于我有什么?

当我运行此代码时,出现此错误消息:

UnexpectedTagNameException                Traceback (most recent call last)
<ipython-input-73-242d18138a79> in <module>
     30 
     31 e = driver.find_element_by_xpath('//*[@id="trading-modal-root"]')
---> 32 Select(e).select_by_value('selected').click()
     33 
     34 

~\anaconda3\lib\site-packages\selenium\webdriver\support\select.py in __init__(self, webelement)
     37             raise UnexpectedTagNameException(
     38                 "Select only works on <select> elements, not on <%s>" %
---> 39                 webelement.tag_name)
     40         self._el = webelement
     41         multi = self._el.get_attribute("multiple")

UnexpectedTagNameException: Message: Select only works on <select> elements, not on <div>

我从这个网站得到的解决方案:https://intellipaat.com/community/4266/how-to-select-a-drop-down-menu-option-value-with-selenium-python

那里的对话是这样的:

I need to select an element from a drop-down menu.

For example:

<select id="fruits01" class="select" name="fruits">

  <option value="0">Choose your fruits:</option>

  <option value="1">Banana</option>

  <option value="2">Mango</option>

</select>

First I have to click on it. I do this:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

After that I have to select the good element, let us say Mango.

I tried to do it with inputElementFruits.send_keys(...) but it did not work.

有答案

In my opinion, unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click. 

Select the element and enumerate the options, selecting the option(s) you want. For example,

from selenium import webdriver

b = webdriver.Firefox()

b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()

For more information please go through the following tutorial to get more info about Selenium:

Selenium presents a convenient Select class to work with select -> option constructs:

from selenium import webdriver

from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()

driver.get('url')

select = Select(driver.find_element_by_id('fruits01'))

# select by visible text

select.select_by_visible_text('Banana')

# select by value 

select.select_by_value('1')

所以我尝试将该解决方案添加到我的代码中。

有没有可能,我首先需要 select 下拉菜单的“可见” div 部分,然后才能 select select 或者 select来自 html?

的部分

谢谢大家,我感谢任何帮助 本杰明

使用

打开模态对话框后
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@data-description='AKER CARB.CAPT.AS NK1']"))).click()

您可以执行以下操作:
至select配额:

wait.until(EC.presence_of_element_located((By.XPATH, "//div[@id='trading-modal-root' and(contains(@style,'block'))]//select[@class='js-order-type-select order-type-selector']")))
select1 = Select(driver.find_element_by_xpath("//div[@id='trading-modal-root' and(contains(@style,'block'))]//select[@class='js-order-type-select order-type-selector']"))

# select by value "limit"
select1.select_by_value('limit')
#or "quote"
select1.select_by_value('quote')
#or "stop"
select1.select_by_value('stop')

#to select purchase or sale action:
select2 = Select(driver.find_element_by_xpath("//div[@id='trading-modal-root' and(contains(@style,'block'))]//select[@class='js-order-type-buysell order-buysell-selector']"))

# select by value "buy"
select2.select_by_value('buy')
#or "sell"
select2.select_by_value('sell')