在选择列表Selenium中选择2nd <li>元素

时间:2017-12-21 15:31:49

标签: python html selenium webdriver picklist

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

4 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

基于其他人帮助最简单(可能不是最好)的方法是确保单击选项列表字段本身,然后我能够找到元素[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()

答案 2 :(得分:0)

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

  • 您已识别出WebElement并尝试按以下方式调用click()

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

    但是<li> optionDropDown中的selenium.common.exceptions.ElementNotVisibleException: Message: element not visible 相似。因此,您看到:

    innerHTML
  • 接下来,您已提取上述WebElement的{​​{1}},其中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() ,如下所示:

     while (.10 <= diff < .25)
    

答案 3 :(得分:0)

正如我所见,您已经为每个 li 授课,这是选择第二个 li 或说某个特定孩子正在使用 css_selector < / strong>方法。

driver.find_element_by_css_selector("ul .rcbItem:nth-child(2)").click()

上面的代码将选择(11)November - 2017