Python Selenium Button不会点击

时间:2017-08-29 19:52:22

标签: python selenium button xpath

我试图点击此Go按钮但它还没有工作。

这是HTML:

<a name="DERIVED_SSS_SCL_SSS_GO_1" id="DERIVED_SSS_SCL_SSS_GO_1" role="button" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win1(document.win1,'DERIVED_SSS_SCL_SSS_GO_1');"><img src="https://sims-prd.sfu.ca/cs/csprd/cache/PT_NAV_GO_1.gif" name="DERIVED_SSS_SCL_SSS_GO_1$IMG" alt="Go" title="Go" border="0"></a>

我尝试过以下方法:

driver.find_element_by_xpath('//*[@id="DERIVED_SSS_SCL_SSS_GO_1"]').click()
driver.find_element_by_xpath("//a[contains(@href,'win1')]").click()
driver.find_element_by_id('DERIVED_SSS_SCL_SSS_GO_1').click()

感谢任何帮助。 感谢。

编辑:包含更多HTML,希望能够缩小我的问题范围。 我必须遗漏这里所包含的内容。

<td height="28"></td>
<td colspan="4" align="left" valign="top">
<div id="win1div$ICField281">
<table cols="1" class="PABACKGROUNDINVISIBLEWBO" cellspacing="0" cellpadding="2" width="163">
<tbody><tr>
<td class="PAGROUPBOXLABELINVISIBLE" align="left">Group Box</td>
</tr>

<tr>
<td width="161">
<table role="presentation" id="ACE_$ICField281" cols="3" class="PABACKGROUNDINVISIBLE" style="border-style:none" cellspacing="0" cellpadding="0" border="0" width="161">
<tbody><tr>
<td width="3" height="0"></td>
<td width="132"></td>
<td width="26"></td>
</tr>

<tr>
<td colspan="2" height="1"></td>
<td rowspan="2" align="left" valign="top" nowrap="nowrap">
<div id="win1divDERIVED_SSS_SCL_SSS_GO_1"><a name="DERIVED_SSS_SCL_SSS_GO_1" id="DERIVED_SSS_SCL_SSS_GO_1" role="button" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win1(document.win1,'DERIVED_SSS_SCL_SSS_GO_1');"><img src="https://sims-prd.sfu.ca/cs/csprd/cache/PT_NAV_GO_1.gif" name="DERIVED_SSS_SCL_SSS_GO_1$IMG" alt="Go" title="Go" border="0"></a><!-- DERIVED_SSS_SCL_SSS_GO_1 --></div>
</td>
</tr>

<tr>
<td height="22"></td>
<td align="left" valign="top">
<div id="win1divDERIVED_SSS_SCL_SSS_MORE_ACADEMICS"><select name="DERIVED_SSS_SCL_SSS_MORE_ACADEMICS" id="DERIVED_SSS_SCL_SSS_MORE_ACADEMICS" size="1" class="PSDROPDOWNLIST" style="width:132px; ">
<option value="">&nbsp;</option>
<option value="4010">Academic Planner</option>
<option value="3010">Academic Requirements</option>
<option value="2015">Apply/Cancel Graduation</option>
<option value="SFU4">Appointment Booking</option>
<option value="SFU5">Confirmation of Enrollment</option>
<option value="2050">Course History</option>
<option value="SFU7">Credential Completion Letter</option>
<option value="1002">Enrollment Activity</option>
<option value="1020">Exam Schedule</option>
<option value="SFU1">Link to SFU Canvas</option>
<option value="SFU2">Prev. Requested Reports</option>
<option value="SFU6">Transcript: Advising</option>
<option value="SFU3">Transcript: Official</option>
<option value="2035">Transcript: Unofficial</option>
<option value="2025">Transfer Credit: Report</option>
<option value="9999" selected="selected">other academic...</option>
</select></div>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</div>
</td>

2 个答案:

答案 0 :(得分:0)

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

selector = "//a[@id='DERIVED_SSS_SCL_SSS_GO_1']"

首先尝试:

element = WebDriverWait(driver, 5).until(
    EC.visibility_of_element_located((By.XPATH, selector))
)
element.click()

如果它不起作用,请给它一个机会:

element = WebDriverWait(driver, 5).until(
    EC.element_to_be_clickable((By.XPATH, selector))
)
element.click()

答案 1 :(得分:0)

尝试使用此代码,看看它是否有效 -

 element = driver.find_element_by_id("DERIVED_SSS_SCL_SSS_GO_1")
 driver.execute_script("arguments[0].click();", element)

更新

element = driver.find_element_by_xpath('//*[@id="DERIVED_SSS_SCL_SSS_GO_1"]/img')
driver.execute_script("arguments[0].click();", element)