代码之家  ›  专栏  ›  技术社区  ›  Ossama

搜索带有selenium no id no name的文本

  •  0
  • Ossama  · 技术社区  · 6 年前

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

    我正在寻找的所需文本:

    PCA提交请求成功完成

    部分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> 
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Andrei Suvorkov    6 年前

    可以使用以下命令定位元素:

    //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)
    
        2
  •  0
  •   cruisepandey    6 年前

    你可以试试这个 XPath :

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

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

    如你所说,你必须满足某些条件才能尝试 显式等待 thread.sleep(time for waiting) 在里面 爪哇 time.sleep(5) 在里面 蟒蛇 不过是最坏的情况 显式等待 .