选择第二个选项列表中的元素Selenium

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选择第二个选项列表中的元素Selenium相关的知识,希望对你有一定的参考价值。

我遇到了不同的错误。目标是设置一个选项列表选项,以便具有各种统计信息更新的报告页面。选项列表是选择月份和年份,每当我想要这个报告时,它就是前一个月所以我一直想要的选项是前几个月最终是li#2。

所以我有以下内容

ReportPeriodInput = DriverTransactionSummary.find_element_by_xpath('//*[@id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_DropDown"]/div/ul/li[2]')

如果我做的事情

ReportPeriodInput.click()

我收到以下错误

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

如果我改变我的方式并添加get_attribute('innerhtml

ReportPeriodInput = DriverTransactionSummary.find_element_by_xpath('//*[@id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_DropDown"]/div/ul/li[2]').get_attribute('innerHTML')
ReportPeriodInput.click()

我收到以下错误

AttributeError:'str'对象没有属性'click'

选择此元素并将其作为选项列表中的新值的正确方法是什么?请参阅下面的HTML页面详细信息

<div id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod" class="RadComboBox RadComboBox_Windows7 RadComboBoxWithLabel" style="white-space:normal;">
<label for="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_Input" class="rcbLabel">Period:</label>
<table summary="combobox" border="0" style="border-width:0;border-collapse:collapse;width:250px">
    <tr class="rcbReadOnly">
        <td class="rcbInputCell rcbInputCellLeft" style="width:100%;"><input name="dnn$ctr1001$VolumeByMonthCS$rcbPeriod" type="text" class="rcbInput radPreventDecorate" id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_Input" value="(12)December - 2017" readonly="readonly" /></td>
        <td class="rcbArrowCell rcbArrowCellRight"><a id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a></td>
    </tr>
</table>
<div class="rcbSlide" style="z-index:6000;display:none;">
    <div id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Windows7 ">
        <div class="rcbScroll rcbWidth">
            <ul class="rcbList">
                <li class="rcbItem">(12)December - 2017</li>
                <li class="rcbItem">(11)November - 2017</li>
                <li class="rcbItem">(10)October - 2017</li>
                <li class="rcbItem">(09)September - 2017</li>
                <li class="rcbItem">(08)August - 2017</li>
                <li class="rcbItem">(07)July - 2017</li>
                <li class="rcbItem">(06)June - 2017</li>
                <li class="rcbItem">(05)May - 2017</li>
                <li class="rcbItem">(04)April - 2017</li>
                <li class="rcbItem">(03)March - 2017</li>
                <li class="rcbItem">(02)February - 2017</li>
                <li class="rcbItem">(01)January - 2017</li>
                <li class="rcbItem">(12)December - 2016</li>
                <li class="rcbItem">(11)November - 2016</li>
                <li class="rcbItem">(10)October - 2016</li>
                <li class="rcbItem">(09)September - 2016</li>
                <li class="rcbItem">(08)August - 2016</li>
                <li class="rcbItem">(07)July - 2016</li>
                <li class="rcbItem">(06)June - 2016</li>
                <li class="rcbItem">(05)May - 2016</li>
                <li class="rcbItem">(04)April - 2016</li>
                <li class="rcbItem">(03)March - 2016</li>
                <li class="rcbItem">(02)February - 2016</li>
                <li class="rcbItem">(01)January - 2016</li>
                <li class="rcbItem">(12)December - 2015</li>
                <li class="rcbItem">(11)November - 2015</li>
                <li class="rcbItem">(10)October - 2015</li>
                <li class="rcbItem">(09)September - 2015</li>
                <li class="rcbItem">(08)August - 2015</li>
                <li class="rcbItem">(07)July - 2015</li>
                <li class="rcbItem">(06)June - 2015</li>
                <li class="rcbItem">(05)May - 2015</li>
                <li class="rcbItem">(04)April - 2015</li>
                <li class="rcbItem">(03)March - 2015</li>
                <li class="rcbItem">(02)February - 2015</li>
                <li class="rcbItem">(01)January - 2015</li>
                <li class="rcbItem">(12)December - 2014</li>
                <li class="rcbItem">(11)November - 2014</li>
                <li class="rcbItem">(10)October - 2014</li>
                <li class="rcbItem">(09)September - 2014</li>
                <li class="rcbItem">(08)August - 2014</li>
                <li class="rcbItem">(07)July - 2014</li>
                <li class="rcbItem">(06)June - 2014</li>
                <li class="rcbItem">(05)May - 2014</li>
                <li class="rcbItem">(04)April - 2014</li>
                <li class="rcbItem">(03)March - 2014</li>
                <li class="rcbItem">(02)February - 2014</li>
                <li class="rcbItem">(01)January - 2014</li>
                <li class="rcbItem">(12)December - 2013</li>
                <li class="rcbItem">(11)November - 2013</li>
                <li class="rcbItem">(10)October - 2013</li>
                <li class="rcbItem">(09)September - 2013</li>
                <li class="rcbItem">(08)August - 2013</li>
                <li class="rcbItem">(07)July - 2013</li>
                <li class="rcbItem">(06)June - 2013</li>
                <li class="rcbItem">(05)May - 2013</li>
                <li class="rcbItem">(04)April - 2013</li>
                <li class="rcbItem">(03)March - 2013</li>
                <li class="rcbItem">(02)February - 2013</li>
                <li class="rcbItem">(01)January - 2013</li>
                <li class="rcbItem">(12)December - 2012</li>
                <li class="rcbItem">(11)November - 2012</li>
                <li class="rcbItem">(10)October - 2012</li>
                <li class="rcbItem">(09)September - 2012</li>
                <li class="rcbItem">(08)August - 2012</li>
                <li class="rcbItem">(07)July - 2012</li>
                <li class="rcbItem">(06)June - 2012</li>
                <li class="rcbItem">(05)May - 2012</li>
                <li class="rcbItem">(04)April - 2012</li>
                <li class="rcbItem">(03)March - 2012</li>
                <li class="rcbItem">(02)February - 2012</li>
                <li class="rcbItem">(01)January - 2012</li>
                <li class="rcbItem">(12)December - 2011</li>
                <li class="rcbItem">(11)November - 2011</li>
                <li class="rcbItem">(10)October - 2011</li>
                <li class="rcbItem">(09)September - 2011</li>
                <li class="rcbItem">(08)August - 2011</li>
                <li class="rcbItem">(07)July - 2011</li>
                <li class="rcbItem">(06)June - 2011</li>
                <li class="rcbItem">(05)May - 2011</li>
                <li class="rcbItem">(04)April - 2011</li>
                <li class="rcbItem">(03)March - 2011</li>
                <li class="rcbItem">(02)February - 2011</li>
                <li class="rcbItem">(01)January - 2011</li>
                <li class="rcbItem">(12)December - 2010</li>
                <li class="rcbItem">(11)November - 2010</li>
                <li class="rcbItem">(10)October - 2010</li>
                <li class="rcbItem">(09)September - 2010</li>
                <li class="rcbItem">(08)August - 2010</li>
                <li class="rcbItem">(07)July - 2010</li>
                <li class="rcbItem">(06)June - 2010</li>
                <li class="rcbItem">(05)May - 2010</li>
                <li class="rcbItem">(04)April - 2010</li>
                <li class="rcbItem">(03)March - 2010</li>
                <li class="rcbItem">(02)February - 2010</li>
                <li class="rcbItem">(01)January - 2010</li>
                <li class="rcbItem">(12)December - 2009</li>
                <li class="rcbItem">(11)November - 2009</li>
                <li class="rcbItem">(10)October - 2009</li>
                <li class="rcbItem">(09)September - 2009</li>
                <li class="rcbItem">(08)August - 2009</li>
                <li class="rcbItem">(07)July - 2009</li>
                <li class="rcbItem">(06)June - 2009</li>
                <li class="rcbItem">(05)May - 2009</li>
                <li class="rcbItem">(04)April - 2009</li>
                <li class="rcbItem">(03)March - 2009</li>
                <li class="rcbItem">(02)February - 2009</li>
                <li class="rcbItem">(01)January - 2009</li>
                <li class="rcbItem">(12)December - 2008</li>
                <li class="rcbItem">(11)November - 2008</li>
                <li class="rcbItem">(10)October - 2008</li>
                <li class="rcbItem">(09)September - 2008</li>
                <li class="rcbItem">(08)August - 2008</li>
                <li class="rcbItem">(07)July - 2008</li>
                <li class="rcbItem">(06)June - 2008</li>
                <li class="rcbItem">(05)May - 2008</li>
                <li class="rcbItem">(04)April - 2008</li>
                <li class="rcbItem">(03)March - 2008</li>
                <li class="rcbItem">(02)February - 2008</li>
                <li class="rcbItem">(01)January - 2008</li>
                <li class="rcbItem">(12)December - 2007</li>
                <li class="rcbItem">(11)November - 2007</li>
                <li class="rcbItem">(10)October - 2007</li>
                <li class="rcbItem">(09)September - 2007</li>
                <li class="rcbItem">(08)August - 2007</li>
                <li class="rcbItem">(07)July - 2007</li>
                <li class="rcbItem">(06)June - 2007</li>
                <li class="rcbItem">(05)May - 2007</li>
                <li class="rcbItem">(04)April - 2007</li>
                <li class="rcbItem">(03)March - 2007</li>
                <li class="rcbItem">(02)February - 2007</li>
                <li class="rcbItem">(01)January - 2007</li>
                <li class="rcbItem">(12)December - 2006</li>
                <li class="rcbItem">(11)November - 2006</li>
                <li class="rcbItem">(10)October - 2006</li>
                <li class="rcbItem">(09)September - 2006</li>
                <li class="rcbItem">(08)August - 2006</li>
                <li class="rcbItem">(07)July - 2006</li>
                <li class="rcbItem">(06)June - 2006</li>
                <li class="rcbItem">(05)May - 2006</li>
                <li class="rcbItem">(04)April - 2006</li>
                <li class="rcbItem">(03)March - 2006</li>
                <li class="rcbItem">(02)February - 2006</li>
                <li class="rcbItem">(01)January - 2006</li>
                <li class="rcbItem">(12)December - 2005</li>
                <li class="rcbItem">(11)November - 2005</li>
                <li class="rcbItem">(10)October - 2005</li>
                <li class="rcbItem">(09)September - 2005</li>
                <li class="rcbItem">(08)August - 2005</li>
                <li class="rcbItem">(07)July - 2005</li>
                <li class="rcbItem">(06)June - 2005</li>
                <li class="rcbItem">(05)May - 2005</li>
                <li class="rcbItem">(04)April - 2005</li>
                <li class="rcbItem">(03)March - 2005</li>
                <li class="rcbItem">(02)February - 2005</li>
                <li class="rcbItem">(01)January - 2005</li>
                <li class="rcbItem">(12)December - 2004</li>
                <li class="rcbItem">(11)November - 2004</li>
                <li class="rcbItem">(10)October - 2004</li>
                <li class="rcbItem">(09)September - 2004</li>
                <li class="rcbItem">(08)August - 2004</li>
                <li class="rcbItem">(07)July - 2004</li>
                <li class="rcbItem">(06)June - 2004</li>
                <li class="rcbItem">(05)May - 2004</li>
                <li class="rcbItem">(04)April - 2004</li>
                <li class="rcbItem">(03)March - 2004</li>
                <li class="rcbItem">(02)February - 2004</li>
                <li class="rcbItem">(01)January - 2004</li>
            </ul>
        </div>
    </div>
</div>
<input id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_ClientState" name="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_ClientState" type="hidden" />

以及看起来像的图像:

enter image description here

答案

我对python不是很熟悉,但知道它在其他语言中是如何工作的,你应该能够使用.find_elements_by_css_selector()获取元素列表,然后单击列表中的第二个元素。

options = DriverTransactionSummary.find_elements_by_css_selector(".rcbItem")
options[1].click();

当然,您需要先单击下拉菜单才能单击菜单中的第二个li元素。希望这可以帮助。

另一答案

基于其他人帮助最简单(可能不是最好)的方法是确保单击选项列表字段本身,然后我能够找到元素[2]。

ReportPeriodClick = DriverTransactionSummary.find_element_by_xpath('//*[@id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_Input"]').click()
ReportPeriodClick2 = DriverTransactionSummary.find_element_by_xpath('//*[@id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_DropDown"]/div/ul/li[2]').click()
另一答案

让我们首先分析您的代码试验发生了什么。

  • 你已经确定了一个WebElement并试图调用click()如下: ReportPeriodInput = DriverTransactionSummary.find_element_by_xpath('//*[@id="dnn_ctr1001_VolumeByMonthCS_rcbPeriod_DropDown"]/div/ul/li[2]') ReportPeriodInput.click() 但这个<li>看起来像option中的DropDown。因此你看到: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  • 接下来你提取了上面的innerHTMLWebElement,它返回一个String并试图在它上面调用click()click()方法是为WebElement定义的。由于String类对象没有关联的click()方法,因此您会看到错误: AttributeError: 'str' object has no attribute 'click'

方案:

要点击DropDown并选择(11)November - 2017选项,你必须:

  • 确定Dropdown WebElement并点击它如下: ReportPeriodInput = DriverTransactionSummary.find_element_by_xpath("//div[@class='RadComboBoxDropDown RadComboBoxDropDown_Windows7' and contains(@id,'VolumeByMonthCS_rcbPeriod_DropDown')]") ReportPeriodInput.click()
  • 接下来,您需要点击option中的预期Dropdown,如下所示: ReportPeriodInputOption = DriverTransactionSummary.find_element_by_xpath("//div[@class='RadComboBoxDropDown RadComboBoxDropDown_Windows7' and contains(@id,'VolumeByMonthCS_rcbPeriod_DropDown')]/div/ul/li[@class='rcbItem' and contains(.,'November - 2017')]") ReportPeriodInputOption.click()

以上是关于选择第二个选项列表中的元素Selenium的主要内容,如果未能解决你的问题,请参考以下文章

每当我更改第一个下拉列表中的选项时,需要 jquery 将第二个下拉列表的默认选项更改为“选择”

在tablayout viewpager中运行调整选项卡片段

两个片段之间的 onItemClickListener

如何进行列表框选择并根据该选择创建第二个列表框?

Android片段中的EditText值不刷新

使用 jQuery 根据第一个选择列表选项更改第二个选择列表