代碼示例:css
#!/usr/bin/python # -*- coding: UTF-8 -*- # coding:utf8 import sys import os from selenium import webdriver sys.path.append(os.getcwd()) driver = webdriver.Chrome('/Users/fyh/tool/chromedriver') # Optional argument, if not specified will search path. #注意此處的chromedriver須要與瀏覽器的版本匹配,不然會報錯 # options=webdriver.ChromeOptions() # options.add_argument('disable-infobars') driver.set_page_load_timeout(30) driver.implicitly_wait(30) #登陸 driver.get("https://h5.daily.weidian.com/m/zhaoshang-h5/apply.html?id=30898")#打開連接 driver.find_element_by_id("J_phone").send_keys("13876543566")#找到控件併發送文字 driver.find_element_by_id("J_password").send_keys("123456") driver.find_element_by_id("J_submit").click()#找到控件並點擊按鈕 driver.find_elements_by_class_name("common_button")[0].click()#經過find_elements_by_class_name找控件 text=driver.find_element_by_xpath("//*[@id=\"app\"]/div/div/div/section[1]/div[1]/span").text#經過find_element_by_xpath找控件 if text == "未經過": print("經過") else: print("不經過") sleep(2) driver.quit()
其餘方法:html
find_element_by_name() find_element_by_css_selector()