学习记录:selenium3百度弹出框关闭

    科技2024-10-06  21

    import time from selenium import webdriver driver = webdriver.Chrome() url = 'https://www.baidu.com/' driver.get(url) driver.maximize_window() time.sleep(2) # 定位到百度首页的"设置"文本链接 el_set = driver.find_element_by_id('s-usersetting-top') el_set.click() time.sleep(2) # 点击"搜索设置"按钮 el_search = driver.find_element_by_class_name('setpref') el_search.click() time.sleep(2) # 定位到"保存设置"按钮 el_save = driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]') el_save.click() time.sleep(5) # 进入警告框中,并且点击接受 driver.switch_to.alert.accept() # 进入警告框中,并且解散警告框 #driver.switch_to.alert.dismiss() #关闭提示框 time.sleep(5) driver.quit() # ps: # 1.list中的元素可以用xpath的相对路径来定位; # 2.点击完某个元素一定要加上time.sleep(2)让它缓冲一下,否则一定报错.
    Processed: 0.008, SQL: 8