如何使用 Selenium-Python 从多 select 列表中 select 多个选项?

How to select multiple options from multi select list using Selenium-Python?

我正在尝试 select P0_ENGLISHP1_ENGLISHP5_ENGLISH 来自多个 select,其中有 10 个选项。我只想select这3个选项。

HTML 代码:

<select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;">
    <option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option>
    <option title="Generic_Hindi" value="Generic_Hindi">Generic_Hindi</option>
    <option title="P0_English" value="P0_English">P0_English</option>
    <option title="P0_Hindi" value="P0_Hindi">P0_Hindi</option>
    <option title="P1_English" value="P1_English">P1_English</option>
    <option title="P1_Hindi" value="P1_Hindi">P1_Hindi</option>
    <option title="P4_English" value="P4_English">P4_English</option>
    <option title="P4_Hindi" value="P4_Hindi">P4_Hindi</option>
    <option title="P5_English" value="P5_English">P5_English</option>
    <option title="P5_Hindi" value="P5_Hindi">P5_Hindi</option>
</select>

SELENIUM-PYTHON 代码:

queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues.select_by_visible_text("P5_English"

我试过使用此代码。使用此代码,我可以 select 第一个选项,即 "P0_ENGLISH"。但是,在 select 选择第一个选项后,我得到一个错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

在 Selenium 的上下文中,当引用无效时,引用是 stale,因为引用的元素已被删除,或者由于元素已分离然后附加而过时通过客户端脚本。在不了解客户端脚本的精确机制的情况下,可能会有不同的解决方案。最简单的方法是尝试再次引用该元素,即

queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
queues.select_by_visible_text("P0_English")
time.sleep(3)
queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
queues.select_by_visible_text("P1_English")
time.sleep(3)
queues = Select(driver.find_element_by_css_selector(".rowStyle1:nth-child(6).gwtListBox"))
queues.select_by_visible_text("P5_English")

这假定 CSS 选择器在重新附加选择列表后保持不变。也有可能选择器变得无效,因为元素已被删除或它的位置已更改。在第一种情况下,您希望抛出异常并适当地处理它,而在第二种情况下,您可以凭经验或通过客户端脚本代码分析找出其新选择器是什么。有关 StaleElementReferenceException here.

的更多信息

到 select 多个 选项 来自 多 Select 元素你可以使用 ActionChains 模拟 Control Click 如下:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

myElemA = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P0_English']")
myElemB = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P1_English']")
myElemC = driver.find_element_by_css_selector(".rowStyle1:nth-child(6) .gwt-ListBox option[value='P5_English']")
ActionChains(driver).key_down(Keys.CONTROL).click(myElemA).key_up(Keys.CONTROL).perform()
ActionChains(driver).key_down(Keys.CONTROL).click(myElemB).key_up(Keys.CONTROL).perform()
ActionChains(driver).key_down(Keys.CONTROL).click(myElemC).key_up(Keys.CONTROL).perform()

OP 是 select 多 select 列表中的部分项目,但如果你想 select 列表中的所有项目,那么这里是选项。

JavaScript:

elements = driver.find_elements_by_css_selector(".gwt-ListBox option")
driver.execute_script("arguments[0].forEach(function(ele){ele.selected=true;});",elements)

Pyhton

elements = driver.find_elements_by_css_selector(".gwt-ListBox option")
for ele in elements:
    # select the item here
For me:

Multi-select option present on Techlistic form site worked by below code when I used CSS Selector-

https://www.techlistic.com/p/selenium-practice-form.html

act=ActionChains(self.drv)
WE_cmd= self.drv.find_element(By.CSS_SELECTOR,'#selenium_commands > option:nth-child(2)' )
opt=Select(self.drv.find_element(By.ID,"selenium_commands"))
opt.select_by_visible_text("Browser Commands")
act.key_down ( Keys.CONTROL ).click ( WE_cmd).key_up ( Keys.CONTROL ).perform ()