折腾:
【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作
期间,需要去模拟点击按钮
所以需要先找到按钮,再模拟点击某个按钮
selenium send_keys
轻松自动化—selenium-webdriver(python) (十二) – 虫师 – 博客园
“#也可定位登陆按钮,通过enter(回车)代替click()
driver.find_element_by_id(“login”).send_keys(Keys.ENTER)“
去试试
然后通过:
nextPageElement = driver.find_element_by_class_name(“n”) print nextPageElement nextPageElement.send_keys(Keys.ENTER) |
是可以实现找到对应的按钮:
并模拟点击,然后搜索结果就切换到下一页了:
但是有个问题:
【总结】
至此已实现想要的,加载页面,知道某个按钮出现,然后模拟点击该按钮了:
#!/usr/bin/python # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 import time driver = webdriver.Chrome() url = ‘http://www.baidu.com/‘ driver.get(url) print “driver.title=%s”%(driver.title) assert u”百度” in driver.title searchElement = driver.find_element_by_name(“wd”) searchElement.send_keys(“crifan”) searchElement.send_keys(Keys.RETURN) print “searchElement=%s”%(searchElement) # print driver.page_source try: print “before wait: %s”%(time.strftime(‘%Y%m%d %H:%M:%S’,time.localtime(time.time()))) nextPageElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, “n”))) print “after wait: %s”%(time.strftime(‘%Y%m%d %H:%M:%S’,time.localtime(time.time()))) except: print u”can not find 下一页” driver.quit() # nextPageElement = driver.find_element_by_class_name(“n”) print “nextPageElement=%s”%(nextPageElement) nextPageElement.send_keys(Keys.ENTER) |
最后页面的效果:
转载请注明:在路上 » 【已解决】Python中Selenium中如何实现模拟点击按钮