Ada*_*ien 146 python selenium webdriver web-scraping selenium-webdriver
我需要从下拉菜单中选择一个元素.
例如,打开这个:
<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>
Run Code Online (Sandbox Code Playgroud)
所以首先我要点击它.我这样做:
inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()
Run Code Online (Sandbox Code Playgroud)(好吧,打开菜单)
Mango但它没有用.ale*_*cxe 258
Selenium提供了一个方便的Select类来使用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')
# select by value
select.select_by_value('1')
Run Code Online (Sandbox Code Playgroud)
也可以看看:
ala*_*ing 89
除非你的点击激活某种类型的ajax调用来填充你的列表,否则你实际上不需要执行点击.
只需找到元素然后枚举选项,选择所需的选项即可.
这是一个例子:
from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
Run Code Online (Sandbox Code Playgroud)
您可以在以下网址阅读更多信息:https:
//sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver
tvs*_*s89 35
我希望这段代码能帮到你。
from selenium.webdriver.support.ui import Select
Run Code Online (Sandbox Code Playgroud)
带有 id 的下拉元素
ddelement= Select(driver.find_element_by_id('id_of_element'))
Run Code Online (Sandbox Code Playgroud)
带有 xpath 的下拉元素
ddelement= Select(driver.find_element_by_xpath('xpath_of_element'))
Run Code Online (Sandbox Code Playgroud)
带有 css 选择器的下拉元素
ddelement= Select(driver.find_element_by_css_selector('css_selector_of_element'))
Run Code Online (Sandbox Code Playgroud)
从下拉菜单中选择“香蕉”
ddelement.select_by_index(1)
ddelement.select_by_value('1')
ddelement.select_by_visible_text('Banana')
小智 22
首先,您需要导入Select类,然后您需要创建Select类的实例.创建Select类的实例后,您可以在该实例上执行select方法以从下拉列表中选择选项.这是代码
from selenium.webdriver.support.select import Select
select_fr = Select(driver.find_element_by_id("fruits01"))
select_fr.select_by_index(0)
Run Code Online (Sandbox Code Playgroud)
小智 6
我尝试了很多东西,但我的下拉是在一张桌子里,我无法执行简单的选择操作.只有以下解决方案有效.在这里,我突出显示下拉元素并按下箭头直到获得所需的值 -
#identify the drop down element
elem = browser.find_element_by_name(objectVal)
for option in elem.find_elements_by_tag_name('option'):
if option.text == value:
break
else:
ARROW_DOWN = u'\ue015'
elem.send_keys(ARROW_DOWN)
Run Code Online (Sandbox Code Playgroud)
您无需单击任何内容。使用 xpath 查找或您选择的任何内容,然后使用发送密钥
对于您的示例:HTML:
<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>
Run Code Online (Sandbox Code Playgroud)
Python:
fruit_field = browser.find_element_by_xpath("//input[@name='fruits']")
fruit_field.send_keys("Mango")
Run Code Online (Sandbox Code Playgroud)
就是这样。
根据提供的 HTML:
<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>
Run Code Online (Sandbox Code Playgroud)
要从html-select菜单中选择一个<option>元素,您必须使用Select Class。而且,你必须与互动下拉菜单,你必须诱导WebDriverWait的。 element_to_be_clickable()
要从下拉列表中选择<option>文本为芒果,您可以使用以下任一定位器策略:
使用ID属性和select_by_visible_text()方法:
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
from selenium.webdriver.support.ui import Select
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "fruits01"))))
select.select_by_visible_text("Mango")
Run Code Online (Sandbox Code Playgroud)
使用CSS-SELECTOR和select_by_value()方法:
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.select[name='fruits']"))))
select.select_by_value("2")
Run Code Online (Sandbox Code Playgroud)
使用XPATH和select_by_index()方法:
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//select[@class='select' and @name='fruits']"))))
select.select_by_index(2)
Run Code Online (Sandbox Code Playgroud)
您可以很好地使用 css 选择器组合
driver.find_element_by_css_selector("#fruits01 [value='1']").click()
Run Code Online (Sandbox Code Playgroud)
将 attribute = value css 选择器中的 1 更改为与所需水果对应的值。
小智 5
通过这种方式,您可以选择任何下拉列表中的所有选项。
driver.get("https://www.spectrapremium.com/en/aftermarket/north-america")
print( "The title is : " + driver.title)
inputs = Select(driver.find_element_by_css_selector('#year'))
input1 = len(inputs.options)
for items in range(input1):
inputs.select_by_index(items)
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
209875 次 |
| 最近记录: |