Selenium multi-form operation with multiple windows, and warning box handling

Source: Internet
Author: User

Knowledge needs to be constantly brush up, otherwise it is easy to forget.

I used to operate the IFRAME, multi-window, feel very simple. Six months did not operate automation, knowledge and return back.

Blogging has a benefit, you can remember the knowledge points recorded, so that after their own to look at the time, you can quickly find ideas.

After all, I am not a great God, so I still record more.

One, multi-form switching:

Example:

Driver.switch_to.iframe ("If" ")

Driver.find_element_by_css_selector ("[name=kw]."). Click ()

Switch_to.frame () By default, the Id,name property of the form can be taken directly. If the IFRAME does not have an available Id,name property, it can be positioned in the following way.

Xf=driver.find_element_by_css_selector (". Class") #先通过CSS定位到iframe

Driver.switch_to.frame (XF) # #再将定位对象传给switch_to. Frame () method.

Driver.find_element_by_css_selector (". Class") # # #再来操作元素.

Driver.switch_to.parent_frame () # # #完成了再当前表单上的操作, jump out of the current level form.

Multi-window operation:

From selenium import Webdriver
Driver=webdriver. Chrome ()
Driver.maximize_window ()
Driver.implicitly_wait (10)
Import time
Driver.get ("https://www.baidu.com/")

#获取当前页面句柄
Current_handle=driver.current_window_handle
Driver.find_element_by_css_selector ("Div#u1>a:nth-child (7)"). Click ()
Driver.find_element_by_link_text (U "Register Now"). Click ()
Time.sleep (1)

#获得当前所有打开窗口的句柄
All_handles=driver.window_handles

#进入注册页面窗口
For handle in All_handles:
If Handle!=current_handle:
Driver.switch_to_window (handle)
Driver.find_element_by_css_selector ("[Name=username]"). Clear ()
Driver.find_element_by_css_selector ("[Name=username]"). Send_keys ("UserName")
Driver.find_element_by_css_selector ("[Name=phone]"). Clear ()
Driver.find_element_by_css_selector ("[Name=phone]"). Send_keys (1313313)
Time.sleep (1)

#回到百度搜索页面
For handle in All_handles:
If Handle==current_handle:
Driver.switch_to_window (Current_handle)
Driver.find_element_by_css_selector ("#TANGRAM__PSP_4__closeBtn"). Click ()
Time.sleep (1)
Driver.find_element_by_css_selector ("#kw"). Send_keys ("Selenium")
Driver.find_element_by_css_selector ("#su"). Click ()
Time.sleep (2)

Driver.quit ()

Directly on the example, look more intuitive.

Selenium multi-form operation with multiple windows, and warning box handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.