如何通过Selenium Python 2.7修复Chrome中的Gmail安全错误



我想在Chrome中使用Selenium Python 2.7打开Gmail帐户。我能够成功登录到Gmail帐户。但是,有时,我可以得到Its you verify按摩,然后它会要求提供电子邮件ID或电话号码以验证使用该帐户的问题。那么,我们可以使用if-else条件或其他东西吗?我如何使用if-else条件,因为它是在建立登录到gmail帐户时验证步骤。请向我建议任何其他方法来解决我的问题。谢谢我无法使用此查询附加屏幕截图(stackoverflow.com 不允许我附加任何图像)。

请提供其他条件来解决此问题。谢谢

您可以检查 not 上是否存在"它您验证"元素,然后可以继续执行所需的步骤。

List <Webelement> verify = driver.findElemnts(By.xpath("//*[contains(text(),'Its you verify')]"));
if(verify.count>0)
{
  //Do verify action
}

上面提到的 xpath 只是一个例子,您可以在使用它之前进行验证。

跳出框框思考...为什么要费心使用Selenium在浏览器中驱动Gmail? 如果您不测试Gmail的浏览器GUI本身,那么我建议您使用Gmail API来获取邮件。

我有另一个无需任何喧嚣的解决方案。将Seleniumwire与未检测到的浏览器v2一起使用

注意:将 chromedriver 放在您的系统路径中。

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time
options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
browser = Chrome(seleniumwire_options=options, options=chrome_options)
browser.get('https://gmail.com')
browser.find_element_by_xpath('//*[@id="identifierId"]').send_keys('your-email')
browser.find_element_by_xpath('//*[@id="identifierNext"]/div/button').click()
time.sleep(5)
browser.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys('you-password')
browser.find_element_by_xpath('//*[@id="passwordNext"]/div/button').click()

除此之外,硒线还有许多很棒的功能,请查看 Github 存储库

相关内容

最新更新