简体   繁体   English

使用 Python Selenium 在页面上查找文本

[英]Find text on a page using Python Selenium

Can I find the element on the page by the text: "Confirmation link:", and copy from there only what comes after the ":"?我可以通过文本找到页面上的元素:“确认链接:”,然后只从那里复制“:”之后的内容吗?

How it looks on the page:它在页面上的外观:

<div class="msg-body P_wpofO mq_AS" data-test-id="message-view-body-content"><div class="jb_0 X_6MGW N_6Fd5"><div><div id="yiv5768405894">Hello,
<br>
<br>
Thank you for registering at FaucetPay. However, before you getting running on the site, you've to confirm your email address. Click <b><a rel="nofollow noopener noreferrer" target="_blank" href="http://email.ml.faucetpay.io/c/eJxNT7uOxCAM_JpQRmBjAgXFNfcbJ7CNklOyrHJJsX9_dLvSFPPQ2BrNLgSXgiUiI7l4FGCzZbAANqJz5AFp5pJSTc0xuEXVyeTtsc-t3KzXs7zmrZs1F5RolZhJSbVFkAW91eLrsujIzJ7X63r-Tfg1wffAZ3_IwtzvxzUY90fbzuPn7UhtpJVEkaJ6jKxYbIgQxnmEyhUkSErixzdE11C9F63JNiRoUMyZuR_HvR961u1Xx4BXWXufh_sPy8pPlQ">here</a></b> to confirm your account, or copy the link below directly to confirm your email address.
<br>
<br>
<b>Confirmation link: https://faucetpay.io/account/confirm_account/...</b>
<br>
<br>                
Regards,
<br>
FaucetPay
<br>
<br>
<small>If you didn't apply for an account, please ignore this email and you won't be bugged again.</small><img width="1px" height="1px" alt="" src="https://ecp.yusercontent.com/mail?url=http%3A%2F%2Femail.ml.faucetpay.io%2Fo%2FeJwNzDEOwyAMQNHTlBFhGztl4DBgjJIqiCpqhty-jP8N3zKIQJLAzK7lEqmhuiNjQAxvAuCIxF5LSjV1UITNDNorhnH6Xm6137c8_phuzwWlEzL3rQLE3pSFVimrCkgjd2WdY9znsKseH1uTp-xz-qV_3GYn9A&amp;t=1661961265&amp;ymreqid=21542e5c-ba48-29be-1cc7-00000001a100&amp;sig=6gRNlw6iByYWJxMZjHioTA--~D">
</div></div></div></div>

You need to first identify the element using xpath and then get the text of the element.您需要首先使用xpath识别元素,然后获取元素的文本。 Then split() the text with "Confirmation link:" which will return an zero based array and you need to get the last index of that array.然后使用“确认链接:” split()文本,这将返回一个从零开始的数组,您需要获取该数组的最后一个索引。

Code:代码:

messageText=driver.find_element(By.XPATH , "//div[@data-test-id='message-view-body-content']//b[contains(., 'Confirmation link')]").text
linkFromText=messageText.split("Confirmation link:")[-1]
print(linkFromText.strip())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM