Skip to content

Just Code

Share Codes I Am Using or Studying




Menu
  • 首页
  • PHP Mysql 网站开发
    • Php / Pear / Mysql / Node.js
    • WordPress / Magento / Shopify
  • Python 编程
  • Java 编程
    • Phone / IOS / Objective-C / Swift
    • JavaSE
    • Java / Jsp
  • Linux Shell 编程
    • Mac / Github / Shell / Linux
    • Tools
  • JS 前端
    • Javascript /Jquery / React / Bootstrap
    • HTML5 / CSS / SVG
    • Google / Facebook / Amazon
  • AI 机器学习
  • Movies 电影电视剧线上看
  • 跨境电商
September 27, 2019
HomePython / WxpythonPython: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗, How to access popup login window in selenium using Python

Python: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗, How to access popup login window in selenium using Python

By Gideon Python / Wxpython  0 Comments

 

许多网站使用社交媒体登录来简化用户的登录过程。在大多数情况下,如果单击该按钮,则会打开一个新的弹出窗口,用户必须在其中输入用户凭据。可以手动在浏览器中切换窗口并输入所需的凭据以登录。但是,如果使用webdriver进行无人值守的Web访问,则驱动程序不能仅自动切换窗口。我们需要更改驱动程序中的窗口句柄,以便在弹出窗口中输入登录凭据。Selenium具有使用同一驱动程序切换窗口以访问多个窗口的功能。

 

首先,我们必须从Webdriver获取当前的窗口句柄,这可以通过以下方式完成:

driver.current_window_handle

 

我们需要保存它以获取当前的窗口句柄。弹出窗口出现后,我们必须立即获取所有可用窗口句柄的列表。

driver.window_handles

 

然后,我们可以从该列表中获取登录页面的窗口句柄,然后切换控件。要切换窗口句柄,请使用:

driver.swtich_to.window(login_page)

成功登录后,我们可以使用相同的switch_to 方法将控制权更改为上一页。

注意:要运行此代码,需要selenium库和用于firefox的geckodriver。可以使用Python第三方库安装程序完成selenium的安装pip。要安装硒,请运行以下命令

pip install selenium

对于geckodriver,下载文件并将其路径添加到OS PATH变量,以便可以从文件目录中的任何位置激活它。

 

让我们看看使用Facebook在zomato.com上登录的代码。

# import the libs 
from selenium import webdriver 
from time import sleep 

# create the initial window 
driver = webdriver.Firefox() 

# go to the home page 
driver.get('https://www.zomato.com') 

# storing the current window handle to get back to dashbord 
main_page = driver.current_window_handle 

# wait for page to load completely 
sleep(5) 

# click on the sign in tab 
driver.find_element_by_xpath('//*[@id ="signin-link"]').click() 

sleep(5) 

# click to log in using facebook 
driver.find_element_by_xpath('//*[@id ="facebook-login-global"]/span').click() 

# changing the handles to access login page 
for handle in driver.window_handles: 
	if handle != main_page: 
		login_page = handle 
		
# change the control to signin page		 
driver.switch_to.window(login_page) 

# user input for email and password 
print('Enter email id : ', end ='') 
email = input().strip() 
print('Enter password : ', end ='') 
password = input().strip() 

# enter the email 
driver.find_element_by_xpath('//*[@id ="email"]').send_keys(email) 

# enter the password 
driver.find_element_by_xpath('//*[@id ="pass"]').send_keys(password) 

# click the login button 
driver.find_element_by_xpath('//*[@id ="u_0_0"]').click() 



# change control to main page 
driver.switch_to.window(main_page) 

sleep(10) 
# print user name 
name = driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/header/div[2]/div/div/div/div/span').text 
print('Your user name is : {}'.format(name)) 

# closing the window 
driver.quit() 

 

输出:

Python: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗, How to access popup login window in selenium using Python
Python: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗, How to access popup login window in selenium using Python

 

 

本文:Python: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗, How to access popup login window in selenium using Python

Loading

Related Posts

  • Python:Python数据验证库 – validus, python 数据验证, python 判断数据类型, python verification

      Validus A dead simple Python data validation library.…

  • Python: webdriver操作select下拉选项, selenium设定select选项, selenium select dropdown, Handling Dropdowns
    Python: webdriver操作select下拉选项, selenium设定select选项, selenium select dropdown, Handling Dropdowns

        下拉菜单是Internet上Web表单的固有部分。如果必须从可用选项列表中进行选择,则实现此目标的最佳方法是下拉菜单。一个非常普通的例子是您的出生年份,即某年某月某日。您将看到一个可滚动的下拉列表,该下拉列表允许您从其他各种元素的列表中选择单个元素。 下拉列表本身并不是单个元素。它实际上是一个程序集或一类元素。例如,您可以说国家是类,下拉列表中可用的值是各种对象。 要使用下拉菜单,首先需要选择或找到主要元素组,然后进一步进入内部并选择要为其选择的子元素。   选择一个下拉菜单 由于下拉菜单有很多选项,因此我们必须单独选择某些内容,这一点很重要。Selenium Python…

  • Python 读取gmail, Python 搜索gmail, Python操作gmail, How to access Gmail using Python

        步骤1:开启Gmail API 中文版: 开启Gmail API 首先,您需要启用Gmail API并从您的Google帐户获取所需的OAuth凭据。步骤如下所示。…

Tags:How to access popup login window in selenium using Python, Python: 访问Selenium中的弹出式登录窗口, webdriver访问弹出窗

Related Posts

Python 操作 google sheet, Python 读写 Google Sheets, Accessing Google Spreadsheet Data using Python

Python 操作 google sheet, Python 读写 Google Sheets, Accessing Google Spreadsheet Data using Python

Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python

Python:如何使用Selenium在IFrame之间切换, Selenium获取IFrame, webdriver, How to Switch Between IFrames Using Selenium Python

Python: 转换Python默认版本, 设置Python3为默认版本, How to make 'python' program command execute Python 3?

python的字典dirc和json的区别, dict(或对象)与json之间的互相转化

About Author

Gideon

Add a Comment

Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

站内搜索

多语言转换

Recent Posts

  • 谷歌表格, 谷歌表格公式, 如何计算 Google 表格中两个日期之间的差异, How to Calculate the Difference Between Two Dates in Google Sheets
  • 干货│Amazon Post怎么用?免费的引流利器你还不会?Amazon Post是什么? 如何创建亚马逊POST呢?
  • Xorg模式切换, 解决Shutter失效问题, Ubuntu 22.04 Xorg模式, How to enable/disable wayland on Ubuntu 22.04 Desktop
  • 升级到Ubuntu22之后运行 php7.4 , php7.4 in my Apache in Ubuntu 22, Tutorial Install and Configure PHP on Ubuntu 22.04,tu 22.04 run php7.4
  • Ubuntu上安装微信(wechat), wechat for ubuntu, weixin for ubuntu, ubuntu版微信, ubuntu版wechat

站内搜索




Tags

2021完整版 2022完整版 Chrome插件 css FullCalendar git Google Cloud Platform HTML java javascript javase jquery json linux 自动化 List lunar machine learning magento magneto MySQL php python qq Shell: expect - 自动交互脚本 Shell脚本 shell自动化 Spring Boot Ubuntu WebDriverIO 入门 WebDriverIO 教程 wordpress 买车必读 二手车 人工智能学习 农历 吴京 技术 文咏珊 时间戳 易烊千玺 电影 线上看 网页爬虫 节日 节气




Categories

  • AI 机器学习
  • Apache / VPN / Software
  • Codeigniter / CakePHP
  • Div / Css / XML / HTML5 / SVG
  • Google / Facebook / Amazon / Twitter / Network
  • Java / Jsp
  • Javascript /Jquery / React / Bootstrap / Web
  • JavaSE
  • movies
  • Others
  • Perl / Koha / Ruby / Markdown
  • Phone / IOS / Objective-C / Swift
  • Php / Pear / Mysql / Node.js
  • Python / Wxpython
  • SEO
  • Tools
  • Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux
  • Videos
  • WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra
  • 二手车
  • 会计
  • 跨境电商




Recent Comments

  • Splinter基本用法, python splinter用法, python自动化, Splinter实例, python网络爬虫, python selenium, python selenium框架 - Just Code on Xpath cheatsheet, XPath用法详解
  • Selenium webdriver 读取认证码, 读取亚马逊Amazon认证码, helium webdriver读取验证码, python读取图形验证码, Solve text captcha, python_anticaptcha - Just Code on helium详细教程, 轻量级Selenium webdriver, Python 自动化, helium安装升级, Helium实现web的自动化, 氦气, Helium文档
  • 簡詩樂見蘇七提到這件事,她先俏皮的朝顧子承做了個鬼臉,而後才認真下來,開口回道。 – Neftetreid on 100 个免费翻墙工具
  • 夜景辰不是已經死了么?他二王兄也親自去認過屍,確定從塞靈河中尋到的屍體就是夜景辰。 – Starving Vendors on 100 个免费翻墙工具
  • WebDriverIO 教程六:在 Selenium 中处理下拉列表, Handling Dropdown In Selenium, WebDriverIO 教程, WebDriverIO 入门 - Just Code on WebDriverIO 教程五:Selenium 定位器实例, Uses Selenium Locators in a Unique Way, WebDriverIO 教程, WebDriverIO 入门







  • 首页
  • PHP Mysql 网站开发
    • Php / Pear / Mysql / Node.js
    • WordPress / Magento / Shopify
  • Python 编程
  • Java 编程
    • Phone / IOS / Objective-C / Swift
    • JavaSE
    • Java / Jsp
  • Linux Shell 编程
    • Mac / Github / Shell / Linux
    • Tools
  • JS 前端
    • Javascript /Jquery / React / Bootstrap
    • HTML5 / CSS / SVG
    • Google / Facebook / Amazon
  • AI 机器学习
  • Movies 电影电视剧线上看
  • 跨境电商




Just Code Copyright © 2023.
Theme by iKeepStudying. Back to Top ↑
Please support this website by adding us to your whitelist in your ad blocker. Ads are what helps us bring you premium content! Thank you!