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

我想选择P0_ENGLISHP1_ENGLISHP5_ENGLISH从多个选择其中有10个选项。我只想选择这三个选项。

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键代码:

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"

我尝试使用此代码。使用此代码,我可以选择第一个选项,即“ P0_ENGLISH”。但是,选择第一个选项后,我得到一个错误:

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

回答:

要从 Multi Select 元素中选择多个 选项 ,可以使用 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()

以上是 如何使用Selenium-Python从多重选择列表中选择多个选项? 的全部内容, 来源链接: utcz.com/qa/423147.html

回到顶部