简体   繁体   中英

Python - AttributeError: 'List' Object has no attribute 'click'. Selenium. (Picture provided)

I'm sure there's something simple I'm not figuring out but I need help with this. I'm trying to select an item within the cell of a table but it's proving difficult. Below is the source:

  <div class="clear formrow top_border">
            <div>
<table class="infoGrid" cellspacing="0" cellpadding="0" border="0" id="ctl00_cphMain_gdvSchdRel" style="border-collapse:collapse;">
    <tr>
        <th scope="col">Program Name</th><th scope="col">
                            <img alt="Schedule Type" src="../../../../Resources/images/arrow_black_downward.gif" />
                        </th><th scope="col">Week Of</th><th scope="col">Schedule Distribution</th><th scope="col">Release Date/Time</th><th scope="col">Init Cap</th><th scope="col">CapLift Date/Time</th><th scope="col">Max Cap</th><th scope="col">&nbsp;</th>
    </tr><tr>
        <td>Disney Mainline</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchTyp','')"></a>
                        </td><td>Oct 22, 2017</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchDist" class="cls_arw arw_grey" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchDist','')"></a>
                        </td><td>Oct 10, 2017 10:21 AM</td><td>15</td><td>Oct 12, 2017 03:30 PM</td><td>80</td><td>
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Id" value="138" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Sch_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Sch_Id" value="48344" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Grp_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Grp_Id" value="686" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnWk_Strt_Date" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnWk_Strt_Date" value="10/22/2017 00:00:00" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnOffset" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnOffset" value="-300" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnPrivType" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnPrivType" value="e" />
                        </td>
    </tr><tr>
        <td>Disney Mainline</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl03_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl03$lnkSchTyp','')"></a>
                        </td><td>Oct 29, 2017</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl03_lnkSchDist" class="cls_arw arw_grey" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl03$lnkSchDist','')"></a>
                        </td><td>Oct 17, 2017 06:00 PM</td><td>15</td><td>Oct 19, 2017 03:30 PM</td><td>80</td><td>
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Id" value="138" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Sch_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Sch_Id" value="48471" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Grp_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Grp_Id" value="686" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnWk_Strt_Date" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnWk_Strt_Date" value="10/29/2017 00:00:00" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnOffset" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnOffset" value="-300" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnPrivType" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnPrivType" value="e" />
                        </td>
    </tr>
</table>
</div>
    </div>

As you can tell since the table hasn't been closed that there are more rows. They look exactly the same except for the id is different. These are the things that I've tried so far:

elem = driver.find_element_by_id("ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp")
elem.click()

elem=driver.find_element_by_css_selector("a.cls_arw arw_blue")
elem.click()

for row in driver.find_elements_by_css_selector("a.cls_arw arw_blue"):
    cell = row in row.find_elements_by_class_name("cls_arw arw_blue")
    cell.click()

elem = driver.find_elements_by_class_name("cls_arw arw_blue")[-1]
elem.click()

The last two things that I tried are because as I pointed out there are more rows to the table that look exactly the same except for the id is different. I wanted to always grab the last blue arrow in the table, but at this point I would be happy to just figure out how to click on any blue arrow. You can see in the sourcecode that the blue arrow has a href. The image of the table on the webpage is below.

在此处输入图片说明

That first blue arrow is the part of the code that is:

<td><a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchTyp','')"></a></td>

Please let me know if you need more information or if I need to clear something up.

Your description of the problem is not right. Your error was:

AttributeError: 'list' object has no attribute 'click' 

because, as was written in the comments, you tried to click on a list and not an object.

I saw that you edited the question. The original was with:

elem = driver.find_elements_by_id("ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp")
elem.click()

That's why your error.

If you change with:

elem = driver.find_element_by_id("ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp")
elem.click()

You will see that the error is another.

If you take a look to your element:

<a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchTyp','')"></a>

There is the attribute href that contains the ref to the javascript to execute.

If you want to perform this action, try:

elem = driver.find_element_by_id('ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp')
href = elem.get_attribute("href")
print(href)
#href="alert('hello world');"
driver.execute_script(href, elem)

Going through all the comments/discussions/Answer I think we have over complicated the issue. The error 'List' Object has no attribute 'click' says it all clear and concise that we are trying to invoke click() method on a List object which is not supported.

If your requirement was to click on each and every link we would have used find_elements_by_css_selector and iterated through the list and clicked on each WebElement in the List . But as your requirement is to click on the last blue arrow in the table but you haven't provided the HTML for that. So let us try to figure out how to click on the first blue arrow.

  • xpath :

     driver.find_element_by_xpath("//a[@class='cls_arw arw_blue'][@id,'ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp']").click()
  • css_selector :

     driver.find_element_by_css_selector("a.cls_arw arw_blue[id='ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp']").click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM