简体   繁体   English

如何通过Selenium从下拉列表中选择?

[英]How to select from drop-down list through Selenium?

I'm trying to select a value from a drop-down list that indicates the number of posts that are displayed from the website. 我正在尝试从下拉列表中选择一个值,该列表指示从网站显示的帖子数。

<form method="get" class="forumForm">
        <label for="dispItems">Show items:</label> 
        <select id="dispItems" class="dispItems">
            <option selected="selected">15</option>
            <option>30</option>
            <option>60</option>
            <option>90</option>
            <option>120</option>
            <option>150</option>
        </select>
</form>

I want to change this so that the last option is selected. 我想更改此选项以便选择最后一个选项。 Through other answers I've tried to the following solution: 通过其他答案,我尝试了以下解决方案:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id("dispItems"))

# None of these two options work.
select.select_by_index(4)            # Option 1.
select.select_by_visible_text("150") # Option 2.

Both option 1 and option 2 return the following error message: 选项1和选项2都返回以下错误消息:

WebDriverException: Message: 

If I try: 如果我尝试:

select.select_by_visible_text("random")

I get the following error message: 我收到以下错误消息:

NoSuchElementException: Message: Could not locate element with visible text: random

Edit: The solution, as proposed by KunduK is 编辑:KunduK提出的解决方案是

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH    ,'//select[@id="dispItems"][@class="dispItems"]')))
time.sleep(1)
select=Select(element)
time.sleep(1)
select.select_by_visible_text('150')

Try to use the explicit wait: 尝试使用显式等待:

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


select = Select(ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "dispItems"))))
select.select_by_index(4)

Hope it helps you! 希望它能帮到你!

尝试以下选择最后一项driver.find_element_by_xpath(“(// select ['dispItems'] / option)[last()]”)。click()

Try WebDriverWait this should work. 尝试WebDriverWait这应该工作。

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//select[@id="dispItems"][@class="dispItems"]')))
select=Select(element)
select.select_by_visible_text('150')

OR 要么

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//select[@id="dispItems"][@class="dispItems"]')))
element.click()
element.send_keys(Keys.END)
element.send_keys(Keys.ENTER)

EDITED: 编辑:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//select[@id="dispItems"][@class="dispItems"]')))
element.click()
time.sleep(2)
element.send_keys(Keys.END)
time.sleep(2)
element.send_keys(Keys.ENTER)

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

相关问题 如何在没有下拉列表的情况下使用 Selenium<select> - How to use Selenium with drop-down list that has no <select> 如何使用Selenium和Python从下拉列表中选择一个值 - How to select a value from a drop-down using Selenium and Python 如何通过Selenium和python从下拉菜单中选择元素? - How to select element from drop-down menu via selenium and python? 如何使用硒从下拉列表中选择选项? - How to select option from drop down list with selenium? 获取最新信息<select>Python Selenium 下拉菜单中的值 - getting current <select> value from drop-down menu with Python Selenium 使用 Python 使用 Selenium 选择下拉列表时出现问题 - Problem in selecting a drop-down list with Selenium using Python 如何在 python 中使用 selenium 单击下拉菜单项? - How to click on a drop-down menu item using selenium with python? 无法使用 select 选择下拉选项:UnexpectedTagNameException selenium:python - Not able to select drop-down option using select : UnexpectedTagNameException selenium : python Python TKinter根据另一个下拉列表中的选择创建一个下拉列表 - Python TKinter create a drop-down list dependent on a selection from another drop down list 如何使用 .kv 文件在 kivy 中创建下拉列表 - How to create the drop-down list in kivy with .kv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM