I'm looking to automate my login process to a website that sends a verification code by email every time I try to log in. For this I'm using Python and Selenium.
Logging in to the website and clicking on the email containing the verification code in my gmail is fine but once I'm in the email I struggle to get the identification code.
I tried to use the xpath but the issue is the xpath is always different from a verification code to another.
For instance this is the xpath for one of the verification codes I received by email :
//[@id=":qs"]/div[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[4]/td/div
And another one from a second verification code:
//[@id=":3zm"]/div[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[4]/td/div
As you can see the id is different from one code to another, that's actually the only difference.
So I'm wondering whether I could get the verification code using another element from the HTML.
This is the HTML Code:
<table cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse" class="m_3010061996208616876email-body">
<tbody><tr>
<td height="50" class="m_3010061996208616876resize, m_3010061996208616876tdDefault"></td>
</tr>
<tr>
<td class="m_3010061996208616876subHeader">
Hello,
</td>
</tr>
<tr>
<td height="21" class="m_3010061996208616876tdDefault"></td>
</tr>
<tr>
<td class="m_3010061996208616876emailText"><span class="im">
Your verification code is displayed below. Please use it in the next 15 minutes – after that time it'll expire.
<br><br>
</span><div style="text-align:center;font-size:42px;font-family:PublicoMedium,serif;color:#3e4e61;margin:0 auto;padding:0;line-height:42px">
941976
</div><span class="im">
<br><br>
If you run out of time and need to request a new one, you can do that on the website by clicking 're-send code'.
</span></td>
</tr>
<tr>
<td height="50" class="m_3010061996208616876tdDefault"></td>
</tr>
</tbody></table>
In this one the verification code I'm looking to retrieve is 941976.
I've tried:
browser.find_element_by_xpath("//p[contains(., 'text-align:center;font-size:42px;font-family:PublicoMedium,serif;color:#3e4e61;margin:0 auto;padding:0;line-height:42px')][1]")
but it does not work.
Thanks for your help.
Rémi
You can use css selector below:
td[class*='emailText'] div
Code:
verification_code = driver.find_element_by_css_selector("td[class*='emailText'] div").text.strip()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.