This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

Essential Knowledge of Selenium

Maximizing the Browser window

The maximize_window() function maximizes the simulated browser window

from selenium import webdriver

w=webdriver.Chrome()
w.maximize_window()
w.get('https://www.baidu.com/')
Copy the code

XPath locates

Open the baidu

Locate the input box and enter Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
w=webdriver.Chrome()
w.maximize_window()
w.get('https://www.baidu.com/')
# Locate the input box and enter Selenium
w.find_element(By.XPATH,'//*[@id="kw"]').send_keys('selenium')
Copy the code

CSS locates

Go to Baidu and click

from selenium import webdriver
from selenium.webdriver.common.by import By

w=webdriver.Chrome()
w.maximize_window()
w.get('https://www.baidu.com/')
# Locate the input box and enter Selenium
Send_keys ('selenium') represents the simulated keyboard input selenium
w.find_element(By.XPATH,'//*[@id="kw"]').send_keys('selenium')
#.click() simulates a mouse click
w.find_element(By.CSS_SELECTOR,'#su').click()
Copy the code

Switch to the peer page of the browser

Get handles to all browser Windows
handles = w.window_handles
Switch to the first window
w.switch_to.window(handles[0])
Switch to the last window
w.switch_to.window(handles[-1])
Switch to the second window
w.switch_to.window(handles[1])
Copy the code

Switching embedded pages (pages within pages)

The iframe element creates an inline frame (that is, an inline frame) that contains another document.

The Iframe tag, also known as the floating frame tag, can be used to embed an HTML document in an HTML display. The biggest difference between it and the Frame tag is that the content contained in the embedded <Iframe></Iframe> is an integral part of the entire page, while the content contained in <Frame>< /Frame> is an independent entity and can be displayed independently. In addition, Iframe allows you to display the same content multiple times on the same page without having to repeat the code.Copy the code
w.switch_to.frame('Child page name attribute value or ID attribute value')
Jump back to the outermost page
w.switch_to.default_content()
Copy the code

If there is no id attribute and the name attribute is null, you need to locate the IFrame first.

iframe=w.find_element(By.TAG_NAME,'iframe')
w.switch_to.frame(iframe)
Copy the code