我的問題是:
我想從一個下拉選單中選擇。
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 = "*******"/span>
密碼 = "*******"
url = "https://www.wikifolio.com/dynamic/de/de/login/login?ReturnUrl=/de/de/home&_=1632037782306"/span>
driver = webdriver.Chrome(executable_path=r'UsersBenjaminDownloadschromedriver_win32chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get(url)
driver.find_element_by_name("用戶名").send_keys(用戶名)
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']"))。
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"] ')
---gt; 32 Select(e).select_by_value('selected') .click()
33。
34
~anaconda3libsite-packagesseleniumwebdriversupportselect.py in __init__(self, webelement)
37 raise UnexpectedTagNameException(
38 "Select只對<select>元素有效,對<%s>無效" %。
--> 39 webelement.tag_name)
40 self._el = webelement
41 multi = self._el.get_attribute("multiple")
UnexpectedTagNameException:訊息。Select只對on<select>元素起作用,not on <div>
我從這個網站得到的解決方案。https://intellipaat.com/community/4266/how-to-select-a-drop-down-menu-option-value-with-selenium-python
那里的對話如下:
我需要來選擇一個元素從一個下拉選單。
對于例子。
<select id="fruits01" class="select" name="fruit">
<option value="0">選擇你的水果:</option>。
<option value="1"> Banana</option>
<option value="2">芒果</option>
</select>。
首先我必須要點擊上它。我做這個。
inputElementFruits = driver.find_element_by_xpath("//select[id='fruit']").click()
之后,我必須去選擇好的元素,讓我們說芒果。
我試著去做它用inputElementFruits.send_keys(...),但它沒有不作業。
有了答案
在我看來。除非你的點擊是發射某種的ajax 呼叫 來填充你的串列,你不實際上不需要執行點擊。
選擇元素和列舉選項,選擇你想要的選項(s)。對于例子。
from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']"/span>).click()
對于更多資訊,請通過以下教程來 獲取更多關于Selenium的資訊。
和
Selenium提供了一個方便的Select class to work with select -> option結構。
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')/span>
select by value
select.select_by_value('1')
因此,我試圖將該解決方案添加到我的代碼中。
是否有可能,我首先需要選擇下拉選單的 "可見 "div部分,然后才能從html部分選擇選擇器?
謝謝大家,我很感激任何幫助。 Benjamin
uj5u.com熱心網友回復:
在用
打開模態對話框后wait.until(EC.visibility_of_element_located((By.XPATH, "/a[@data-description='AKER CARB.CAPT.AS NK1']"/span>)).click()
你可以進行以下操作:
要選擇配額:
wait.until(EC.presence_of_element_located((By.XPATH, "//div[@id='trading-modal-root' and(contains(@style, 'block'))]//select[@class='js-order-type-selector']")
select1 = Select(driver.find_element_by_xpath("//div[@id='trading-modal-root' and(contains(@style,'block'))]//select[@class='js-order-type-selector']")
# select by value "limit"/span>
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-buysell order-buysell-selector']")
# select by value "buy")
select2.select_by_value('buy')
#or "sell"
select2.select_by_value('sell')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/327075.html
標籤:
