我正在尝试从下拉列表中选择一个值,该值指示从网站显示的帖子数。
<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>
我要更改此设置,以便选择最后一个选项。通过其他答案,我尝试了以下解决方案:
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.
选项1和选项2都返回以下错误消息:
WebDriverException: Message:
如果我尝试:
select.select_by_visible_text("random")
我收到以下错误消息:
NoSuchElementException: Message: Could not locate element with visible text: random
编辑: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')
答案 0 :(得分:1)
尝试使用显式等待:
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)
希望对您有帮助!
答案 1 :(得分:1)
尝试以下选择最后一项 driver.find_element_by_xpath(“(// select [‘dispItems’] / option)[last()]”)。click()
答案 2 :(得分:1)
尝试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)
已编辑
: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)