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

如何使用Selenium Python从多选列表中选择多个选项?,python,selenium,selenium-webdriver,webdriver,multi-select,Python,Selenium,Selenium Webdriver,Webdriver,Multi Select,我正在尝试从具有10个选项的多个选择中选择P0_ENGLISH,P1_ENGLISH,P5_ENGLISH。我只想选择这3个选项 HTML代码: <select multiple="" class="gwt-ListBox" style="height: 80px; width: 205px;"> <option title="Generic_Eng" value="Generic_Eng">Generic_Eng</option> <o

我正在尝试从具有10个选项的多个选择中选择
P0_ENGLISH
P1_ENGLISH
P5_ENGLISH
。我只想选择这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>
我试过使用这个代码。使用此代码,我可以选择第一个选项,即“P0_ENGLISH”。但是,在选择第一个选项后,我得到一个错误:

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

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

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的详细信息。

要从多选元素中选择多个选项,可以使用ActionChains模拟控件,单击如下所示:

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()

操作是选择多选列表中的部分项目,但如果要选择列表中的所有项目,则以下是选项

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

尝试
element=WebDriverWait(driver,10)。直到(EC.presence\u of_element\u located((By.xpath,title=“P0\u English”)
请编辑您的问题,以便格式正确。
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 ()