[英]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.