【发布时间】:2019-04-02 16:26:03
【问题描述】:
我希望将我的登录过程自动化到一个网站,每次我尝试登录时都会通过电子邮件发送验证码。为此,我使用 Python 和 Selenium。
登录网站并在我的 gmail 中单击包含验证码的电子邮件很好,但是一旦我进入电子邮件,我就很难获得识别码。
我尝试使用 xpath,但问题是 xpath 始终与验证码不同。
例如,这是我通过电子邮件收到的其中一个验证码的 xpath:
//[@id=":qs"]/div[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[4]/td/div
另外一个来自第二个验证码:
//[@id=":3zm"]/div[2]/table/tbody/tr[3]/td/table/tbody/tr/td[2]/table/tbody/tr[4]/td/div
正如您所见,一个代码与另一个代码的 id 不同,这实际上是唯一的区别。
所以我想知道是否可以使用 HTML 中的另一个元素获取验证码。
这是 HTML 代码:
<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>
在这一个中,我要检索的验证码是 941976。
我试过了:
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]")
但它不起作用。
感谢您的帮助。
雷米
【问题讨论】:
标签: python html selenium selenium-webdriver