搜索无标识的文本

2024-06-23 19:31:46 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用selenium/python从下面的html代码中找到以下文本(没有ID、没有名称或引用)。 上述文字仅在满足一定条件后出现(文件上传成功) 参见下面的部分html代码,此文本位于两个表之间,完整代码在link provided

我想要的文本:

PCA Submit Request completed successfully

部分html代码:

<TABLE border=0 cellpadding=0 cellspacing=3>
<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>30071948</TD>
</TR>

<TR>
  <TD class=label>Work Order:</TD>
  <TD class=fieldlabel>1</TD>
</TR>

<TR>
  <TD class=label>Title:</TD>
  <TD class=fieldlabel colspan=3>(NSW) Parramatta - Main Distribution Board LCR Automatic Transfer Switches Design & Construction P2 (PARZ) (IMC154) (44108)</TD>
</TR>
<TR>
  <TD class=label>Description:</TD>
  <TD class=fieldlabel colspan=3>(NSW) Parramatta - Main Distribution Board LCR Automatic Transfer Switches Design & Construction P2 (PARZ) (IMC154) (44108)</TD>
</TR>
<TR>
  <TD class=label>Issue No:</TD>
  <TD class=fieldlabel>2</TD>
  <TD class=label>Status:</TD>
  <TD class=fieldLabel>
    Project Approved</TD>
</TR>


</TABLE>
<!-- Divya : PBI000001666517 - 22Jun2017 release -->


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


    <BR>
    <font color=blue>PCA Submit Request completed successfully.</font>



<P align=left>


<TABLE class="table" border=0 cellspacing=1 cellpadding=0 width="100%">

<TR>
  <TD class="colHeader" colspan=5>Documents associated with the PCA </TD>
  <TD class="cell" align="left" valign="center" colspan=3></TD>
</TR>

<TR>
  <TD class="colHeader">Reference</TD>
  <TD class="colHeader">Title</TD>
  <TD class="colHeader">File Type</TD>
  <TD class="colHeader">Issue No</TD>
  <TD class="colHeader">Size(Bytes)</TD>
  <TD class="colHeader">Date Registered</TD>
  <TD class="colHeader">Registered By</TD>
  <TD class="colHeader">View</TD>
</TR>



<TR class="cell">
  <TD class="cell">CSS-1528090864510</TD>
  <TD class="cell">PCA Documentation</TD>
  <TD class="cell">DOCX</TD>
  <TD class="cell">1</TD>
  <TD class="cell">97904</TD>
  <TD class="cell">04/06/2018</TD>
  <TD class="cell">ADMIN USER</TD>
  <TD valign="center" nowrap>
   <A HREF="javascript:showDocument('40437320', 'CSS-1528090864510')"><P ALIGN=CENTER><IMG SRC='/global/res/images/attach.gif' width=16 height=16 ALT='97904 Bytes' BORDER=0><br>CSS-1528090864510.DOCX</P></A></TD>

</TR>

</TABLE> 

Tags: 代码文本htmltablecellwidthcsslabel
2条回答

可以使用以下方法定位元素:

//font[contains(text(),'PCA Submit Request completed successfully')]

在此元素出现之前,可以设置一个暂停,如示例所示:

from selenium.webdriver.support import expected_conditions

element = WebDriverWait(driver, 10).until(
    expected_conditions.presence_of_element_located((By.xPath, "xPath_to_element"))
)

或者

browser.implicitly_wait(30)

您可以尝试使用xpath:

//font[text()='PCA Submit Request completed successfully.']

//font[contains(text(),'PCA Submit Request completed successfully.')]  

正如您所提到的,必须满足某些条件,您可以在JAVA中尝试explicit waitthread.sleep(time for waiting),在Python中尝试time.sleep(5),这只是explicit wait的最坏情况。你知道吗

相关问题 更多 >

    热门问题