Selenium - 双击选项列表中的项目

时间:2018-05-15 20:00:27

标签: python selenium

我正在尝试自动化我的Web应用程序中的一些表单填充。我有一个多选列表的表单部分,用户从左侧的框中双击他们想要的选项(可用),然后他们被移动到右框(选择)的框中双击。它看起来像这样:

enter image description here

当我检查元素时,左边的框看起来像这样:

enter image description here

我不确定如何使用Python在Selenium中复制此功能。我正在尝试按如下方式获取列表:

driver.find_element_by_id("fframeworkpage:j_id28:sections_1:j_idd0:sections_23:j_idd164:sections_25:j_idd172:field4_unselected")

但我不确定如何在列表中选择一个选项,以及如何在所述选项上加倍。

有什么想法吗?

更新:

enter image description here

enter image description here

更新2:

action = ActionChains(driver)    
parent = driver.find_element_by_xpath("//select[@title='Attendance Options']")
element = parent.find_element_by_xpath("//option[@value='1']")
action.double_click(element).perform()

parent2 = driver.find_element_by_xpath("//select[@title='Services Offered']")
element2 = parent2.find_element_by_xpath("//option[@value='1']")
action.double_click(element2).perform()

1 个答案:

答案 0 :(得分:2)

如果您知道要单击的列表元素的参数,则可以使用元素的XPATH选择器直接选择它,如下所示value='1'例如:

element = driver.find_element_by_xpath("//option[@value='1']")

如果您想点击该元素以将其添加到选择列表,则可以调用click()方法:

element.click()

如果您想要优化搜索并首先选择元素列表,您可以再次使用列表的XPATH知道HTML DOM元素的标签:

list = driver.find_elements_by_xpath("//optgroup[@label='Available']")

然后,您可以使用您选择的方法选择列表中的元素。

要双击您需要使用ActionChain的元素:

from selenium.webdriver.common.action_chains import ActionChains
...
element = ...
actions = ActionChains(driver)
actions.double_click(element).perform()

考虑到您有两个label='Available'的框,您需要更多地优化搜索,方法是在HTML中选择一个图层以选择父级。您可以先使用XPATH选择器再次选择<{1}}元素,知道标题 提供的服务 - 可用

<select ...

考虑到parent = driver.find_element_by_xpath("//select[@title='Services Offered - Available']") 是一个WebElement,您可以调用任何WebElement find 方法来优化您的选择并选择一个子项 - 例如parent.find_element_by_id(...)例如,或parent.find_element_by_xpath(...)。

相关问题