簡體   English   中英

Python - AttributeError: 'List' 對象沒有屬性 'click'。 硒。 (提供圖片)

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

我確定有一些簡單的事情我沒有弄清楚,但我需要幫助。 我試圖在表格的單元格中選擇一個項目,但事實證明這很困難。 下面是源碼:

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

如果您需要更多信息或者我需要澄清一些事情,請告訴我。

您對問題的描述不正確。 你的錯誤是:

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

因為,正如評論中所寫,您試圖單擊列表而不是對象。

我看到你編輯了這個問題。 原文是:

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)

瀏覽所有的評論/討論/回答,我認為我們把問題復雜化了。 錯誤'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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM