我正在尝试从具有10个选项的多重选择中选择P0_ENGLISH
,P1_ENGLISH
,P5_ENGLISH
。我只想选择这三个选项。
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>
硒P代码:
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
答案 0 :(得分:2)
在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的更多信息。
答案 1 :(得分:1)
要从 Multi 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()
答案 2 :(得分:0)
OP是要在多重选择列表中选择部分项,但是如果要选择列表中的所有项,则这里是选项。
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
答案 3 :(得分:0)
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 ()