from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium.webdriver import ActionChains # driver= webdriver.Firefox() driver= webdriver.Chrome() driver.get("https://www.baidu.com/") driver.maximize_window() driver.find_element_by_xpath("//*[@id=\"kw\"]").send_keys("selenium") driver.find_element_by_xpath("//*[@id=\"kw\"]").submit() time.sleep(2) # above=driver.find_element_by_xpath("//span[@id=\"s-usersetting-top\"]") above=driver.find_elements_by_xpath("//*[text()=\"设置\"]")[1] ActionChains(driver).move_to_element(above).perform() SearchSetting_Ele=driver.find_element_by_xpath("//*[text()=\"搜索设置\"][1]") SearchSetting_Ele.click() time.sleep(2) # driver.find_elements_by_xpath("//*[text()='高级搜索']")[1].click() driver.find_element_by_xpath("(//*[text()='高级搜索'])[2]").click() driver.find_element_by_xpath("//*[@id=\"adv_keyword\"]").send_keys("包含全部关键词") driver.find_element_by_xpath("//input[@class=\"c-input adv-q-input switch-input\" and @name=\"q2\"]").send_keys("包含完整关键词") driver.find_element_by_xpath("//input[@class=\"c-input adv-q-input switch-input\" and @name=\"q3\"]").send_keys("包含任意关键词") driver.find_element_by_xpath("//div[@class=\"c-select-selection\"]/i[1]").click() driver.find_element_by_xpath("//p[@class=\"c-select-item\" and @data-for=\"gpc\"][4]").click() driver.find_element_by_xpath("(//i[@class=\"c-icon c-select-arrow\"])[2]").click() driver.find_element_by_xpath("//*[text()=\"所有格式\"]").click() driver.find_element_by_xpath("//*[@id=\"q5_2\"]").click() # driver.find_element_by_xpath("//*[@value=\"高级搜索\"]").click() # time.sleep(2) driver.find_element_by_xpath("//*[@class=\"item\" and @data-tabid=\"general\"]").click() driver.find_element_by_xpath("//*[text()=\"保存设置\"]").click() # ***********操作百度弹出的Alert alert=driver.switch_to.alert print(alert.text) # alert.accept() # alert.send_keys("a") alert.dismiss()