selenium common operations Event 2

1, do not open the browser driving (acceleration)

Note: do not start the browser, you need to place the following browsers driving position:
①, Python installation root directory
②, google installation directory ()

from selenium import webdriver

# Following three acts google browser does not start
the Option = webdriver.ChromeOptions ()
option.add_argument ( "headless")
Driver = webdriver.Chrome (chrome_options = the Option)

driver.get ( "http://www.baidu.com") # access Baidu
print (driver.page_source)

 

2, switching to pop


time.sleep (2) # pop loaded like
a = driver.switch_to.alert # new method, handover Alert
# driver.switch_to_alert A = () # old method, handover Alert
Print (a.text) obtaining pop # text
a.accept () # to confirm, the same as clicking [OK] button
# a.dismiss () # canceled, equivalent to click [cancel] button

 

3, cookie operations

from selenium import webdriver

driver=webdriver.Chrome()
driver.get("https://www.zhihu.com/explore")
print(driver.get_cookies()) #获取cookies
driver.add_cookie({"name":"name","domain":"www.zhihu.com","value":"germey"}) #cookies增加值
print(driver.get_cookies())
driver.delete_all_cookies() #删除所有cookies
print(driver.get_cookies())
browser.close()

 

Guess you like

Origin www.cnblogs.com/whitemouseV2-0/p/11772410.html