【问题标题】:Python - AttributeError: 'List' Object has no attribute 'click'. Selenium. (Picture provided)Python - AttributeError:“列表”对象没有属性“点击”。硒。 (提供图片)
【发布时间】:2018-04-02 17:05:09
【问题描述】:

我确定有一些简单的事情我没有弄清楚,但我需要帮助。我正在尝试在表格的单元格中选择一个项目,但事实证明这很困难。以下是出处:

  <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>

正如您所知道的,由于表尚未关闭,因此还有更多行。它们看起来完全一样,只是 id 不同。这些是我迄今为止尝试过的事情:

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()

我尝试的最后两件事是因为正如我所指出的,表中有更多行看起来完全相同,只是 id 不同。我想总是抓住表格中的最后一个蓝色箭头,但此时我很乐意弄清楚如何单击任何蓝色箭头。您可以在源代码中看到蓝色箭头有一个 href。网页上的表格图片如下。

第一个蓝色箭头是代码的一部分:

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

如果您需要更多信息或我需要澄清一些问题,请告诉我。

【问题讨论】:

  • 您正在使用 findelements(复数形式)并在列表上调用 click 方法。应该在 web 元素上调用 click。使用单数 findelement 或从列表中检索一个元素,然后调用 click()
  • 此外,在你修复elem.click() 之后,我很确定你会在cell.click() 上得到错误。因为您的 cell 似乎是布尔值,而不是 WebElement
  • @Grasshopper,对不起......这是一个错字。我在前两次尝试中使用了 find_element 。我会编辑,但我不确定如何编辑。
  • @Andersson,你看到了什么让你认为单元格是布尔值?
  • @AP1,像something in list 这样的表达式返回TrueFalse。你的意思是cell = [row for row in row.find_elements_by_class_name("cls_arw arw_blue")][0]

标签: python html css selenium selenium-webdriver


【解决方案1】:

您对问题的描述不正确。你的错误是:

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

因为,正如 cmets 中所写,您试图单击列表而不是对象。

我看到您编辑了问题。原件是:

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

这就是你的错误。

如果你改变:

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

你会看到错误是另一个。

如果你看看你的元素:

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

属性 href 包含要执行的 javascript 的引用。

如果您想执行此操作,请尝试:

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)

【讨论】:

  • 嘿@DurdenP,你是对的。当我将元素更改为元素时,我确实得到了一个不同的错误。它抛出了 NoSuchElementException: Unable to locate element。我尝试了您的解决方案,但我收到了同样的错误。您是否认为由于元素是表格中的一个单元格,所以它没有找到它?我一直在研究 xpath,但不确定我是否理解它。
【解决方案2】:

浏览所有 cmets/discussions/Answer 我认为我们已经把问题复杂化了。错误 'List' Object has no attribute 'click' 清楚而简洁地表明我们正在尝试在不受支持的 List 对象上调用 click() 方法。

如果您的要求是单击每个链接,我们将使用 find_elements_by_css_selector 并遍历列表并单击 List 中的每个 WebElement。但是由于您的要求是单击表格中的最后一个蓝色箭头,但您没有为此提供HTML。所以让我们试着弄清楚如何点击第一个蓝色箭头。

  • 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()
    

【讨论】:

  • 您好,谢谢您的回答。当我尝试这个时,我收到 selenium.common.exceptions.InvalidSelectorException: Message: Given xpath expression "//a[@class='cls_arw arw_blue'][@id,'ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp']" is invalid: SyntaxError: The expression is not一种合法的表达方式。我编辑了源代码以显示表格的完整 div。
  • 当我尝试您的第二个解决方案时,我得到一个 NoSuchElementException: Unable to locate element。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2021-07-05
  • 1970-01-01
  • 2021-08-20
  • 2018-01-16
  • 2016-05-14
相关资源
最近更新 更多