driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path. driver.get('http://www.google.com/xhtml'); time.sleep(5) # Let the user actually see something! search_box = driver.find_element_by_name('q') search_box.send_keys('ChromeDriver') search_box.submit() time.sleep(5) # Let the user actually see something! driver.quit()
简单交互
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 加载网页 driver.get("http://www.google.com")
# 获取网页元素 element = driver.find_element_by_id("passwd-id") element = driver.find_element_by_name("passwd") element = driver.find_element_by_xpath("//input[@id='passwd-id']")
# 输入文本框 element.send_keys("some text")
# 输入按键 element.send_keys(" and some", Keys.ARROW_DOWN)
element = driver.find_element_by_xpath("//select[@name='name']") all_options = element.find_elements_by_tag_name("option") for option in all_options: print("Value is: %s" % option.get_attribute("value")) option.click()
# #j_skuwrap > div.skuBox.clearfix.first-skubox.last-skuBox > div > ul > li:nth-child(1) > a driver.find_elements_by_css_selector('#j_skuwrap > div.skuBox.clearfix.first-skubox.last-skuBox > div > ul > li:nth-child(1) > a')[0].click()
问题3:打开一个新的界面,如何跳转
1 2
windows = driver.window_handles driver.switch_to.window(windows[-1])