1 import os 2 import time 3 from selenium import webdriver 4 from selenium.webdriver.support.wait import WebDriverWait 5 from selenium.webdriver.support import expected_conditions as EC 6 from selenium.webdriver.common.by import By 7 from selenium.common.exceptions import NoSuchElementException 8 9 chrome_driver = os.path.abspath(r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe") 10 os.environ["webdriver.chrome.driver"] = chrome_driver 11 driver = webdriver.Chrome(chrome_driver) 12 # driver = webdriver.Chrome() 13 14 driver.get("https://***") 15 driver.maximize_window() 16 try: 17 18 b = WebDriverWait(driver, 5, 0.01).until(lambda x: driver.find_element_by_class_name("ux-modal_close")) 19 except NoSuchElementException as e: 20 print("元素不存在") 21 else: 22 b.click() 23 24 # 此处报错 Other element would receive the click,因为蒙板还没有消失 25 time.sleep(3) 26 try: 27 b = WebDriverWait(driver, 5, 0.01).until(EC.visibility_of_element_located((By.ID, "j-index-nav-login"))) 28 except NoSuchElementException as e: 29 print("元素不存在") 30 else: 31 b.click() 32 33 try: 34 WebDriverWait(driver, 5).until(lambda x: driver.find_element_by_id("ux-modal")) 35 b = WebDriverWait(driver, 5, 0.01).until( 36 lambda x: driver.find_element_by_xpath("//ul[@class='ux-tabs-underline_hd']/li[2]")) 37 except NoSuchElementException as e: 38 print("元素不存在") 39 else: 40 b.click() 41 42 # 隐式等待 43 driver.implicitly_wait(30) 44 iframe = driver.find_element_by_tag_name("iframe") 45 46 47 # 切入 iframe 48 driver.switch_to_frame(iframe) 49 els = driver.find_element_by_xpath("//input[@name='email']") 50 # 利用js将为元素设置焦点 51 driver.execute_script("arguments[0].focus();", els) 52 els.clear() 53 els.send_keys("***") 54 55 driver.find_element_by_class_name("j-inputtext dlemail").send_keys("***") 56 driver.find_element_by_class_name("j-inputtext dlpwd").send_keys("***") 57 driver.find_element_by_class_name("f-cb loginbox").click() 58 59 # 切出 iframe 60 driver.switch_to_default_content()