Python 3 + Selenium:按Xpath查找元素,text()不起作用

时间:2016-09-01 05:10:25

标签: python selenium xpath

这是html代码:

<td>
  <a href="https://www.facebook.com/n/?confirmemail.php&amp;e=myemail%40gmail.com&amp;c=77438&amp;cuid=AYh3nbnRpQs3d3S9QQ4e74G2jZzOBE69YqYVaWlnPtcs-SOpgHEFilr-khC8FsPUB5zzR6rvBQmgU54QxWdjn2jW9A5OhSaUal_KMcpvARnfDzPrmaOE3ObQrn-cfMk0MYFiR8fT0z8HVc3fX328oMpA&amp;medium=email&amp;mid=53b6ce57e8d3bG5af62793fe75G53b6d2f14900dG3c2Gf8c1&amp;bcode=1.1472715008.AbkQwPz9meBC88Kr&amp;n_m=myemail%40gmail.com" style="color:#3b5998;text-decoration:none" target="_blank" data-saferedirecturl="https://www.google.com/url?hl=en&amp;q=https://www.facebook.com/n/?confirmemail.php%26e%3Dmyemail%2540gmail.com%26c%3D77438%26cuid%3DAYh3nbnRpQs3d3S9QQ4e74G2jZzOBE69YqYVaWlnPtcs-SOpgHEFilr-khC8FsPUB5zzR6rvBQmgU54QxWdjn2jW9A5OhSaUal_KMcpvARnfDzPrmaOE3ObQrn-cfMk0MYFiR8fT0z8HVc3fX328oMpA%26medium%3Demail%26mid%3D53b6ce57e8d3bG5af62793fe75G53b6d2f14900dG3c2Gf8c1%26bcode%3D1.1472715008.AbkQwPz9meBC88Kr%26n_m%3Dmyemail%2540gmail.com&amp;source=gmail&amp;ust=1472802324130000&amp;usg=AFQjCNH7AgCibBYBB8I6ECqWXnFcMbwhMw">
    <table style="border-collapse:collapse" cellpadding="0" cellspacing="0" width="100%">
      <tbody>
        <tr>
          <td style="border-collapse:collapse;border-radius:2px;text-align:center;display:block;border:solid 1px #344c80;background:#4c649b;padding:7px 16px 11px 16px">
            <a href="https://www.facebook.com/n/?confirmemail.php&amp;e=myemail%40gmail.com&amp;c=77438&amp;cuid=AYh3nbnRpQs3d3S9QQ4e74G2jZzOBE69YqYVaWlnPtcs-SOpgHEFilr-khC8FsPUB5zzR6rvBQmgU54QxWdjn2jW9A5OhSaUal_KMcpvARnfDzPrmaOE3ObQrn-cfMk0MYFiR8fT0z8HVc3fX328oMpA&amp;medium=email&amp;mid=53b6ce57e8d3bG5af62793fe75G53b6d2f14900dG3c2Gf8c1&amp;bcode=1.1472715008.AbkQwPz9meBC88Kr&amp;n_m=myemail%40gmail.com" style="color:#3b5998;text-decoration:none;display:block" target="_blank" data-saferedirecturl="https://www.google.com/url?hl=en&amp;q=https://www.facebook.com/n/?confirmemail.php%26e%3Dmyemail%2540gmail.com%26c%3D77438%26cuid%3DAYh3nbnRpQs3d3S9QQ4e74G2jZzOBE69YqYVaWlnPtcs-SOpgHEFilr-khC8FsPUB5zzR6rvBQmgU54QxWdjn2jW9A5OhSaUal_KMcpvARnfDzPrmaOE3ObQrn-cfMk0MYFiR8fT0z8HVc3fX328oMpA%26medium%3Demail%26mid%3D53b6ce57e8d3bG5af62793fe75G53b6d2f14900dG3c2Gf8c1%26bcode%3D1.1472715008.AbkQwPz9meBC88Kr%26n_m%3Dmyemail%2540gmail.com&amp;source=gmail&amp;ust=1472802324130000&amp;usg=AFQjCNH7AgCibBYBB8I6ECqWXnFcMbwhMw">
              <center>
                <font size="3">
                  <span style="font-family:Helvetica Neue,Helvetica,Lucida Grande,tahoma,verdana,arial,sans-serif;white-space:nowrap;font-weight:bold;vertical-align:middle;color:#ffffff;font-size:14px;line-height:14px">Confirm&nbsp;Your&nbsp;Account
                  </span>
                </font>
              </center>
            </a>
          </td>
        </tr>
      </tbody>
    </table>
  </a>
</td>

这是我在gmail中单击确认FB链接的代码:

confirm = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, ".//*[text()='Confirm Your Account']")))
confirm.click()

它始终在第一行显示TimeOutException错误。你能修复我的代码吗?谢谢:))

2 个答案:

答案 0 :(得分:0)

尝试检查元素是否包含文本而不是完全匹配

((By.XPATH, ".//*[contains(text(), 'Confirm Your Account')]"))

或者

((By.XPATH, ".//*[contains(., 'Confirm Your Account')]"))

答案 1 :(得分:0)

您可以使用以下xpath

driver.find_element_by_xpath("//td/a[1]")