如何从非选择标签的下拉菜单中选择一个选项? [英] How to select an option from a dropdown of non select tag?
本文介绍了如何从非选择标签的下拉菜单中选择一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从下拉菜单中选择一个值.我尝试了很多在这里找到的解决方案,但是没有用,有时我遇到无法滚动查看的错误.
I am trying to select a value from a dropdown menu. I tried a lot of solutions found here but nothing work, sometimes I have the error can't scroll to view.
代码试用:
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome() # Optional argument, if not specified will search path.
browser.get('https://dzairannonces.com/posts/create');
mySelectElement = browser.find_element_by_id('parentId')
dropDownMenu = Select(mySelectElement)
我想从下拉菜单中选择一个值,然后从第二个下拉菜单中选择另一个值,当我们选择第一个值时出现
I want to select a value from the dropdown form and another value from the second dropdown form that appear when we select the first one
我也尝试过此代码,但无法正常工作
I tried this code too and doesn't work
import unittest
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
class Drpdowm(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_drpdown(self):
driver = self.driver
driver.maximize_window()
driver.get('https://dzairannonces.com/posts/create')
time.sleep(10) # Let the user actually see something!
s1=Select(driver.find_element_by_id('parentId'))
print(s1.options)
for opt in s1.options:
s1.select_by_value(' 315 ')
def tearDown(self):
self.driver.quit()
推荐答案
要选择一个选项,例如从非 <select>
标签的下拉菜单中汽车,您可以使用以下解决方案:
To select an option e.g. Automobiles from a dropdown menu of non <select>
tag you can use the following solution:
-
代码块:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://dzairannonces.com/posts/create')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.select2-selection__rendered#select2-parentId-container"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='select2-results']/ul//li[@class='select2-results__option' and contains(., 'Automobiles')]"))).click()
浏览器快照:
Browser Snapshot:
这篇关于如何从非选择标签的下拉菜单中选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文