使用python3和selenium无法在表中找到链接

时间:2016-11-08 06:05:47

标签: python css selenium xpath

大家好我正在使用Python 3.0,我试图点击表格中的链接。

目前我找不到链接,所以我无法点击它。

以下是所选元素的html

这里的道歉是一个更好的代码示例......

<div id="divMyActivities2" originalheight="125px" style="overflow-y:auto;top:0px;left:0px;width:451px;height:125px;">
        <table id="tblMyActivities" class="podborder" border="0" cellpadding="3" cellspacing="0" style="overflow-y: auto; table-layout: fixed; border: 1px solid #CCCCCC; border-top: none; width: 100%; border-top-left-radius: 0px; border-top-right-radius: 0px">
<tr>
    <td class="customheader" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 100%; color: #004a78; background: #e6ebf1 !important; font-size: 11px;" align="left">&nbsp;&nbsp;<b>Type of Activity</b>
                </td>
    <td class="customheader" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 50px; color: #004a78; background: #e6ebf1 !important; font-size: 11px;" align="center"><b>Past Due</b>
                </td>
    <td class="customheader" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 60px; color: #004a78; background: #e6ebf1 !important; font-size: 11px;" align="center"><b>This Week</b>
                </td>
    <td class="customheader" style="border-bottom: 1px solid #CCCCCC; font-size: 11px; width: 50px; color: #004a78; background: #e6ebf1 !important;" align="center"><b>All</b>
                </td>
</tr>
<tr class="blacktext">
    <td style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; text-align: left; width: 100%;">&nbsp;
                    <img src="../../common/images/cwtoday_bullet.gif" border="0" />
                    <label onclick="pageNav('crmSales');" class="tablelink">CRM / Sales Activities</label>
                </td>
    <td id="tdSalesPastDue" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 50px;" align="center">&nbsp;</td>
    <td id="tdSalesThisWeek" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 60px;" align="center">&nbsp;</td>
    <td id="tdSalesAll" class="tabletext" style="border-bottom: 1px solid #CCCCCC; width: 50px;" align="center">&nbsp;
                </td>
</tr>
<tr id="trProjAct" class="blacktext">
    <td style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; text-align: left; width: 100%; background: #f6f6f6;">&nbsp
                    <img src="../../common/images/cwtoday_bullet.gif" border="0" />
                    <label onclick="pageNav('projectActs');" class="tablelink">Project Tickets</label>
                </td>
    <td id="tdPrjActPastDue" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 50px; background: #f6f6f6;" align="center">&nbsp;</td>
    <td id="tdPrjActThisWeek" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 60px; background: #f6f6f6;" align="center">&nbsp;</td>
    <td id="tdPrjActAll" class="tabletext" style="border-bottom: 1px solid #CCCCCC; width: 50px; background: #f6f6f6;" align="center">&nbsp;</td>
</tr>
<tr class="blacktext">
    <td style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; text-align: left; width: 100%;">&nbsp;
                    <img src="../../common/images/cwtoday_bullet.gif" border="0" />
                    <label onclick="pageNav('projectIssues');" class="tablelink">Project Issues</label>
                </td>
    <td id="tdPrjIssPastDue" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 50px;" align="center">&nbsp;</td>
    <td id="tdPrjIssThisWeek" class="tabletext" style="border-right: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC; width: 60px;" align="center">&nbsp;</td>
    <td id="tdPrjIssAll" class="tabletext" style="border-bottom: 1px solid #CCCCCC; width: 50px;" align="center">&nbsp;</td>
</tr>
<tr class="blacktext">
    <td style="border-right: 1px solid #CCCCCC; border-bottom-left-radius: 6px; text-align: left; width: 100%; background: #f6f6f6;">&nbsp;
                    <img src="../../common/images/cwtoday_bullet.gif" border="0" />
                    <label onclick="pageNav('serviceRequests');" class="tablelink">Service Tickets</label>
                </td>
    <td id="tdSRPastDue" class="tabletext" style="border-right: 1px solid #CCCCCC; width: 50px; background: #f6f6f6;" align="center">&nbsp;</td>
    <td id="tdSRThisWeek" class="tabletext" style="border-right: 1px solid #CCCCCC; width: 60px; background: #f6f6f6;" align="center">&nbsp;</td>
    <td id="tdSRAll" class="tabletext" align="center" style="width: 50px; background: #f6f6f6; border-bottom-right-radius: 6px;">&nbsp;</td>
</tr>

我正在尝试选择此元素:

<label onclick="pageNav('serviceRequests');" class="tablelink">Service Tickets</label>

我试过了......

尝试:

driver.find_element_by_partial_link_text('Tickets').click()

例外:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Tickets"

尝试:

driver.find_element_by_link_text('Service Tickets').click()

例外:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Service Tickets"

试过

driver.find_elements_by_css_selector(".tablelink")

结果:

produces an empty list "[]"

尝试:

driver.find_element_by_tag_name("label")

例外:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"tag name","selector":"label"}

我找到了这个网址,但我无法更改代码:Find and click an item from 'onclick' partial value

  • 狂野猜测我厌倦了从链接中解码

尝试:

driver.find_element_by_css_selector("tablelink[onclick*='Service Tickets']")

例外:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"tablelink[onclick*='Service Tickets']"}

任何帮助都会很棒,提前谢谢。

1 个答案:

答案 0 :(得分:0)

Apolgoes仍在学习这一切。

我不得不切换Iframe然后我能够通过XPATH找到代码。

我不想立即粘贴整个代码,因为它需要很长时间才能完成。

driver.switch_to_frame(driver.find_element_by_class_name("gwt-Frame"))
driver.find_element_by_xpath("//label[contains(.,'Service Tickets')]").click()

感谢您的时间。