python - 使用 Selenium Python 库单击链接

标签 python selenium

如何使用 python selenium 库单击所附图像中突出显示的链接,我已经尝试了所有方法(大多数尝试如下所示),但都不起作用。

注意附图中的元素树。

enter image description here

页面代码(有问题的链接是下面代码中的最后一个链接:

<HEAD>
<TITLE> Constructor Self Service</TITLE>
</HEAD>


<script type="text/javascript">
......
</script>

<link rel="stylesheet"
 href="/global/res/themes/corporate/css/style3.css"
 type="text/css">
<script type="text/javascript"
 src="/global/res/javascript/horizontal_subsection_HM_Loader.js"></script>

<link rel=stylesheet href="css/tcss.css" TYPE="text/css">
<script type="text/javascript" src="arrays/aaclib.js"></script>
<script type="text/javascript" src="arrays/csslib.js"></script>




<table cellspacing=0 cellpadding=0 border=0 width="100%" height="100%" >
<tr><td valign="top">
<form name="mainForm" action="/TCSSPRODapp/Controller"
 method="POST" onsubmit="return false;">
<input type="hidden" name="command">
<input type="hidden" name="page">
<input type="hidden" name="menuIndex">
<input type="hidden" name="submitted" value="false">
<input type="hidden" name="pageToken"
 value=679>
<input type="hidden" name="isInternal"
 value=false>



<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0"
 marginheight="0">
<!--begin global header--><!--Don't place any other HTML code on this line!!-->
<script language="javascript" src="/global/javascript/head_array.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var v6=0;
if(typeof hmVisi!="undefined"&&typeof sectionId!="undefined" )v6=1;
if(v6)document.write('</head><body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="TEL_onLoad()">');

function alertUser() {
    if(confirm("Warning: Any unsaved data would be lost.\rDo you want to Continue?")) {
        return true;
    } else {
        return false;
    }
}

</script>
<link rel="stylesheet" href="/global/css/gwy_hed.css" type="text/css">
<a name="top"></a>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td rowspan="4" valign="top"><a href="http://telstra.com/?tR=1"><img src="/global/images/telstra_logo2.gif" width="85" height="85" border="0" alt="telstra.com"></a></td>
        <td align="right" class="ap1"><span class="hd1"><a href="javascript:toggleAccessibility()"><img src="/global/images/sin.gif" width="1" height="37" border="0" alt="press enter now to toggle the accessibile text mode of this page" align="absmiddle"></a>
        <a href="http://telstra.com/?tR=1">Telstra Homepage</a> | <a href="http://www.telstra.com.au/contact/?tR=1">Contact Us</a> | <a href="http://search.telstra.com/">Search</a><img src="/global/images/sin.gif" width="12" height="1" alt=""></span></td>
        <td width="50%" class="ap1"><img src="/global/images/sin.gif" width="1" height="57" alt=""></td>
    </tr>
    <tr class="hd3">
    <!--<td><img src="/global/images/sin.gif" width="500" height="2" alt=""></td>-->
        <td colspan=50 width="100%"><img src="/global/images/sin.gif" width="100" height="2" alt=""></td>
        <td><img src="/global/images/sin.gif" width="1" height="2" alt=""></td>
    </tr>
    <tr>
        <td align="right"><img src="/global/images/gwy_grad.gif" width="328" height="6" alt=""></td>
        <td class="hd4"><img src="/global/images/sin.gif" width="1" height="1" alt=""></td>
    </tr>
    <tr>
        <td colspan=2 class="hd5" nowrap><NOBR>
    <table cellpadding=1 cellspacing=1 border=0>
  <tr>
   <!-- AMCO-WORKFLOW:MODIFY:START -->
    <!--<td rowspan=3 class="hd5" nowrap><img src="/global/images/sin.gif" width="15" height="10" alt="" align="top">Telstra Contractor Self Service<img src="/global/images/sin.gif" width="25" height="1" alt="" align="top"></td>-->
    <td rowspan=4 class="hd5" nowrap><img src="/global/images/sin.gif" width="5" height="10" alt="" align="top">Telstra Constructor Self Service<img src="/global/images/sin.gif" width="5" height="1" alt="" align="top"></td>
   <!-- AMCO-WORKFLOW:MODIFY:END -->
    <TD nowrap><img src="/global/images/sin.gif" width="10" height="12"></TD>
    <TD nowrap></TD>
    <TD nowrap></TD>
   <!-- AMCO-WORKFLOW:ADD:START -->
   <!--5.14.01 Anand Changes: start-->
    <!--<TD nowrap></TD>-->
    <!--5.14.01 Anand Changes: start-->
   <!-- AMCO-WORKFLOW:ADD:END -->
   <!--5.14.01 Anand Changes: start-->
  </tr>
  <tr style="background-color:#99ccff;">
   <!-- AMCO-WORKFLOW:ADD:START -->

    <TD class=button>
     <a class=button style="text-decoration:none"
     title="AMCO"
     href="javascript:doCommand('doWidebandWorkSummary')"
     onmousemove="window.status='View Work Summary'"
     onmouseout="window.status=window.defaultStatus">
     AMCO</a>
    </TD>

   <!-- AMCO-WORKFLOW:ADD:END -->
    <TD nowrap style="background-color:#ffffff;"></TD>
    <TD class=button nowrap>
     <a class=button style="text-decoration:none"
     title="View Inbox"
     href="javascript:doCommand('doInboxStat')"
     onmousemove="window.status='View Inbox'"
     onmouseout="window.status=window.defaultStatus">
     <NOBR>Inbox 0</NOBR></a>
    </TD>
    <TD class=button>
     <a class=button style="text-decoration:none"
     title="View Outbox"
     href="javascript:doCommand('doOutboxStat')"
     onmousemove="window.status='View Outbox'"
     onmouseout="window.status=window.defaultStatus">
     <NOBR>Outbox 0</NOBR></a>
    </TD>

    <!--Start : IPaC Stage II Drop1 : Refresh button added-->
    <TD class=button>
     <a class=button style="text-decoration:none"
     title="Refresh Inbox and Outbox"
     href="javascript:doCommand('doInboxOutboxRefresh')"
     onmousemove="window.status='Refresh Inbox and Outbox'"
     onmouseout="window.status=window.defaultStatus"
     onclick="return alertUser()">
     <NOBR>Refresh</NOBR></a>
    </TD>
    <!--End : IPaC Stage II Drop1-->

    <TD nowrap style="background-color:#ffffff;"></TD>
    <TD class=button nowrap>
     <a class=button style="text-decoration:none"
     title="Display TCSS help"
     href="javascript:loadHelpWindow()"
     onmousemove="window.status='Display TCSS help'"
     onmouseout="window.status=window.defaultStatus">
     <NOBR>Help</NOBR></a>
    </TD>
      <!-- AMCO-WORKFLOW:ADD:START -->
   <!--5.14.01 Anand Changes: start-->
<!--<TD nowrap width="151" style="background-color:#ffffff;"></TD>-->
       <!--5.14.01 Anand Changes: end-->
  </tr>
  <tr>
    <TD nowrap><img src="/global/images/sin.gif" width="38" height="12"></TD>
    <!--Start : IPaC Stage II Drop1 : Display message if refresh didnt occur-->
    <TD nowrap style="background-color:#ffffff;"></TD>

         <TD nowrap><img src="/global/images/sin.gif" width="38" height="12"></TD>
         <TD nowrap style="background-color:#ffffff;"></TD></tr>
         <tr>
         <TD nowrap><img src="/global/images/sin.gif" width="38" height="12"></TD>
         <TD nowrap style="background-color:#ffffff;"></TD>
         </tr>
        <!--<TD nowrap></TD>-->
       <!--<TD nowrap></TD>-->
       <!-- AMCO-WORKFLOW:ADD:START -->
        <!--<TD nowrap></TD>-->
       <!-- AMCO-WORKFLOW:ADD:END -->

    <!--End : IPaC Stage II Drop1-->
  </tr>
  </table>

  </NOBR>
  </td>

    </tr>
</table>
<script language="javascript" type="text/javascript">

<!--5.14.01 Anand Changes: changed from 100% to 15 % start-->
var s='<table width="50%" cellpadding="0" cellspacing="0" border="0"><tr><td class="siteletTitleTab"><img src="/global/images/sin.gif" width="1" height="3" alt=""></td></tr></table>';
<!--5.14.01 Anand Changes: changed from 100% to 15 % end-->
if(v6)document.write(s);
</script>
<!--end global header--><!--Don't place any other HTML code on this line!!-->





<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr class="">
   <td width="100%" rowspan="2" align=right>

  </td>
<TD>
  <INPUT type="hidden" name="internalConstructor" size=10 value="false"> </TD>
</tr>
</table>

<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr><td colspan=50 class=sitelettitletab>
<img src=/global/res/images/sin.gif width=0 height=3>
</td></tr><tr class=sitelettitletab>

<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doCommand("doFrontPage")' onmousemove="window.status='Home Page'" onmouseout="window.status=window.defaultStatus" title='Home Page'>Home Page</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doCommand("doTransmittalsInbox")' onmousemove="window.status='My TCSS'" onmouseout="window.status=window.defaultStatus" title='My TCSS'>My TCSS</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=siteactivetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=siteactivetabnav valign=top><img src=/global/res/images/sin.gif width=5 height=20></td><td class=siteactivetabnav valign=middle align=center nowrap><img src=/global/res/themes/corporate/images/tick.gif width=8 height=9>&nbsp;<a class=siteactivetabnav onmousemove="window.status='Work Under Contract'" onmouseout="window.status=window.defaultStatus">Work Under Contract</a></td><td align=right class=siteactivetabnav  valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20></td>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doCommand("doGenDocSearch")' onmousemove="window.status='Documents'" onmouseout="window.status=window.defaultStatus" title='Documents'>Documents</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doCommand("doAssetSearch")' onmousemove="window.status='Reference Library'" onmouseout="window.status=window.defaultStatus" title='Reference Library'>Reference Library</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doCommand("doAbout")' onmousemove="window.status='Support'" onmouseout="window.status=window.defaultStatus" title='Support'>Support</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td><img src=/global/res/images/sin.gif width=3 height=1></td><td class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_left_inactive.gif width=8 height=20></td><td class=sitetabnav valign=middle align=center nowrap><a class=sitetabnav href='javascript:doLogout()' onmousemove="window.status='Close TCSS'" onmouseout="window.status=window.defaultStatus" title='Close TCSS'>Close TCSS</a></td><td align=right class=sitetabnav valign=top><img src=/global/res/themes/corporate/images/tab_right_inactive.gif width=8 height=20>
<td width=100%><img src=/global/res/images/sin.gif width=1 height=1></td></tr><tr><td colspan=50><img src=/global/res/images/sin.gif width=0 height=2></td></tr></table>

<table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td>
<img src='/global/res/images/sin.gif' width=1 height=3></td></tr><tr class=sitetabnav><td>
<table cellpadding=0 cellspacing=0 border=0><tr><script type='text/javascript'>
TEL_horizontalSubsectionNav('corporate')
</script><td width=100%>&nbsp;</td></tr></table></td></tr></table>



<table cellspacing=10 cellpadding=0 border=0 width="100%">
<tr><td valign=top width="100%">





<TABLE border=0 cellpadding=0 cellspacing=0 width=100%>
<TR><TD bgcolor=#99ccff>
<TABLE cellspacing=2 cellpadding=0 border=0 width=100%>
<TR><TD bgcolor=#F1F8FE>
 <TABLE border=0 cellpadding=0 cellspacing=0>
 <TR><TD colspan=2 height=1 bgcolor=#F1F8FE></TD>
 <TD rowspan=3><IMG border=0 src='images/corner.jpg'></TD>
 </TR><TR><TD><IMG border=0 width=1 height=1 src='images/whitedot.gif'></TD>
 <TD class='groupboxheader'>&nbsp;&nbsp;WUC CSA -  Scope Variation - Site & Financial Details &nbsp;&nbsp;</TD></TR></TABLE>


<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" WIDTH="100%">
<TR>
  <TD CLASS="label" WIDTH="20%">Contract Number:</TD>
  <TD WIDTH="25%" CLASS="fieldlabel">20003171</TD>
  <TD CLASS="label" WIDTH="20%">Separable Portion:</TD>
  <TD WIDTH="25%" CLASS="fieldlabel">30056120</TD>
</TR>
<TR>
  <TD CLASS="label">Work Order:</TD>
  <TD CLASS="fieldLabel">1</TD>
  <TD CLASS="label">CSA Number:</TD>
  <TD CLASS="fieldLabel">4117298</TD>
</TR>
<TR>
  <TD CLASS="label">Current CSA Status:</TD>
  <TD class="fieldLabel">
     Draft</TD>
  <TD CLASS="label">Initiated By:</TD>
  <TD CLASS="fieldlabel">CONSTRUCTOR</TD>
</TR>
<TR>
  <TD CLASS="label">Issue Number:</TD>
  <TD CLASS="fieldlabel">1</TD>
</TR>
</TABLE>

  </TD>
 </TR>
 </TABLE>
 </TD>
</TR>
</TABLE>

<SCRIPT type="text/javascript">
function showQuote(sequenceNo) {
    var f = document.mainForm;
    switch (parseInt(sequenceNo)) {
    case 10001:
        f.command.value = "doCsaViewPSDetails";
        break;
    case 10002:
        f.command.value = "doCsaViewDRDetails";
        break;
    case 10003:
        f.command.value = "doCsaViewLSDetails";
        break;
    default:
        f.sequenceNo.value = sequenceNo;
        f.command.value = "doCsaViewDIDetails";
    }
    doSubmit(f);
}

function showMaterialQuote(sequenceNo) {
    var f = document.mainForm;
    f.sequenceNo.value = sequenceNo;
    f.command.value = "doCsaViewMatDetails";
    doSubmit(f);
}
//NDCG:For Link to Non Catalogued material screen
//NDCG:Add:Start
function showNonCatMaterialQuote(sequenceNo) {
    var f = document.mainForm;
    f.command.value = "doCsaViewMatExDetails";
    f.sequenceNo.value = sequenceNo;
    doSubmit(f);
}
//NDCG:ADD:End
</SCRIPT>

<INPUT type="hidden" name="sequenceNo">
<BR>

<TABLE border="0" cellspacing="1" cellpadding="0" class="table" width="100%">
<TR align="middle">
  <TD rowspan="2" class=colHeader>Type</TD>
  <TD colspan="3" class=colHeader>TCSS Calculated</TD>
  <TD colspan="3" class=colHeader>Quoted</TD>
  <TD colspan="3" class=colHeader>Approved</TD>
  <TD rowspan="2" class=colHeader>View</TD>
</TR>
<TR align="middle">
  <TD class=colHeader>Value</TD>
  <TD class=colHeader>GST</TD>
  <TD class=colHeader>Price</TD>
  <TD class=colHeader>Value</TD>
  <TD class=colHeader>GST</TD>
  <TD class=colHeader>Price</TD>
  <TD class=colHeader>Value</TD>
  <TD class=colHeader>GST</TD>
  <TD class=colHeader>Price</TD>
</TR>


<TR>
  <TD class=cell>1. PENRITH - Generic Land & Building - Project</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD class=cell align="center" nowrap>
     <A HREF="javascript:showQuote('1')"><IMG SRC="/global/res/images/view_white.gif" width=44 height=18 onmousemove="window.status='View Item'" onmouseout="window.status=window.defaultStatus" ALT="View Item" BORDER=0></A>
     <A HREF="javascript:showMaterialQuote(1)"><IMG SRC=/global/res/images/material.gif width=16 height=16 border=0 onmousemove="window.status='Catalogued Material'" onmouseout="window.status=window.defaultStatus" alt='Catalogued Material'></A>
     <A HREF="javascript:showNonCatMaterialQuote(1)"><IMG SRC=/global/res/images/material.gif width=16 height=16 border=0 onmousemove="window.status='Non Catalogued Material'" onmouseout="window.status=window.defaultStatus" alt='Non Catalogued Material'></A>
  </TD>
</TR>

<TR>
  <TD class=cell>Daywork Rates</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD class=cell align="center" nowrap>
     <A HREF="javascript:showQuote('10002')"><IMG SRC="/global/res/images/view_white.gif" width=44 height=18 onmousemove="window.status='View Item'" onmouseout="window.status=window.defaultStatus" ALT="View Item" BORDER=0></A>


  </TD>
</TR>

<TR>
  <TD class=cell>Lump Sums</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD nowrap class=rightcell>$0.00</TD>
  <TD class=cell align="center" nowrap>
     <A HREF="javascript:showQuote('10003')"><IMG SRC="/global/res/images/view_white.gif" width=44 height=18 onmousemove="window.status='View Item'" onmouseout="window.status=window.defaultStatus" ALT="View Item" BORDER=0></A>


  </TD>
</TR>

尝试:

driver.find_element_by_css_selector("a[href='javascript:showQuote('10003')']").click()
driver.find_element_by_xpath("//a[@href="javascript:showQuote('10003')"]").click()
driver.find_element_by_partial_link_text('showQuote('10003')').click()
driver.find_element_by_css_selector('[href^=javascript:showQuote('10003')]').click()
driver.execute_script('showQuote('10003')')

最佳答案

您是否尝试过使用如下所示的 xpath

   //tbody/tr[5]/td[11]/a/img

我知道如果 tr 和 td 继续变化,那么采取 5、11 之类的位置是不合适的。但尝试一次看看是否能够点击。

你可以这样尝试

  //a[starts-with(@href, 'javascript:showQuote') and contains(@href, '10003')]

如果您使用的是 Firefox 浏览器,请使用带有 firepath 插件的 firebug 来检查它是否正在获取所需的唯一元素。

谢谢你, 穆拉利

关于python - 使用 Selenium Python 库单击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716690/

相关文章:

python - 如何解决 Jupyter Notebook 中 No module named 'hmmlearn' 错误

python - fit_transform() 需要 2 个位置参数,但 3 个是通过 LabelBinarizer 给出的

python - 无法通过在 python 中使用 xpath 和 selenium 来单击元素

Python Splinter 单击按钮 CSS

selenium - 如何使用 selenium 从表内可用的复选框列表中单击复选框

python - 迭代 Pandas 中的列并更改单元格的值 - Python

python - 如何根据最后一行/下一行过滤 Pandas 行?

python - 如何在 python 中对运行 fuzzywuzzy 字符串匹配逻辑的 2m 行进行多重处理?当前的代码非常慢

selenium - 如何编写一个 CSS Selector 选择不具有特定属性的元素?

python - 陈旧元素引用 : element is not attached to the page document with the chrome web driver