我试图从多个选项中选择P0_ENGLISH、P1_ENGLISH、P5_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-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"我试过用这个代码。使用此代码,我可以选择第一个选项,即"P0_ENGLISH“。但是,在选择了第一个选项之后,我得到了一个错误:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document发布于 2019-01-19 21:28:16
要从多个选择元素中选择多个选项,可以使用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()发布于 2019-01-19 16:40:21
在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 这里的报道。
发布于 2019-04-28 14:08:06
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 herehttps://stackoverflow.com/questions/54268992
复制相似问题