Selenium WebDriver 笔记
Selenium 查找 UI 元素(web 元素) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <div id="coolestWidgetEvah" >...</div> WebElement element = driver.findElement(By.id("coolestWidgetEvah" ));<div class="cheese" ><span>Cheddar</span></div><div class="cheese" ><span>Gouda</span></div> List<WebElement> cheeses = driver.findElements(By.className("cheese" )); <iframe src="..." ></iframe> WebElement frame = driver.findElement(By.tagName("iframe" ));<input name="cheese" type="text" /> WebElement cheese = driver.findElement(By.name("cheese" ));<a href="http://www.google.com/search?q=cheese" >cheese</a>> WebElement cheese = driver.findElement(By.linkText("cheese" ));<a href="http://www.google.com/search?q=cheese" >search for cheese</a>> WebElement cheese = driver.findElement(By.partialLinkText("cheese" ));<div id="food" ><span class="dairy" >milk</span><span class="dairy aged" >cheese</span></div> WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged" ));
用户输入 - 填充表单 WebDriver 有一个叫 “Select” 的类,这个类提供了很多有用的方法用于 select 元素进行交互。
1 2 3 Select select = new Select (driver.findElement(By.tagName("select" )));select.deselectAll(); select.selectByVisibleText("Edam" );
上述代码取消页面上第一个 select 元素的所有 option 的选中状态,然后选中字面值为 “Edam” 的 option。
如果你已经完成表单填充,你可能希望提交它,你只要找到 “submit” 按钮然后点击它即可。
driver.findElement(By.id("submit")).click();
或者,你可以调用 WebDriver 为每个元素提供的 “submit” 方法。如果你对一个 form 元素调用该方法,WebDriver 将调用这个 form 的 submit 方法。如果这个元素不是一个 form,将抛出一个异常。element.submit();
常用类与方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait browser = webdriver.Chrome() wait = WebDriverWait(browser, 10 ) browser.get('https://www.baidu.com' ) browser.maximize_window() print (browser.current_url) print (browser.title) print (browser.name) print (browser.current_window_handle) print (browser.get_cookies()) print (browser.page_source) browser.back() browser.forward() browser.refresh() browser.save_screenshot('error.png' ) browser.close() browser.quit()
选择器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait browser = webdriver.Chrome() wait = WebDriverWait(browser, 10 ) browser.get('https://www.baidu.com' ) browser.find_element_by_id('su' ) browser.find_element_by_class_name('xx' ) browser.find_elements_by_class_name('xx' ) browser.find_element_by_link_text('xxx' ) browser.find_element_by_xpath('xxxx' ) browser.find_element_by_tag_name('h1' ) browser.find_elements_by_tag_name('h1' ) browser.find_element_by_css_selector('xxx' )
find_element_by_xpath 测试网页的HTML代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <html > <body > <div id ="div1" style ="text-align:center" > <img alt ="div1-img1" src ="http://www.sogou.com/images/logo/new/sogou.png" href ="http://www.sogou.com" > sogou image</img > <br /> <input name ="div1input" > <a href ="http://www.sogou.com" > 搜狗搜索</a > <input type ="button" value ="查询" > </div > <br > <div id ="div2" style ="text-align:center" > <img alt ="div2-img2" src ="http://www.baidu.com/img/bdlogo.png" href ="http://www.baidu.com" > baidu image</img > <br /> <input name ="div2input" > <a href ="http://www.baidu.com" > 百度搜索</a > <input type ="button" value ="查询" > </div > </body > </html >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 element = driver.find_element_by_xpath('/html/body/div/input[@value="查询"]' ) element = driver.find_element_by_xpath('//input[@value=' 查询']' ) element=driver.find_element_by_xpath("//input[1]" ) img = driver.find_element_by_xpath("//input[@alt='div1-img1' and @href='http://www.sogou.com']" ) elements=driver.find_elements_by_xpath("//img[starts-with(@alt," div1")]" ) elements=driver.find_elements_by_xpath("//img[contains(@alt," img")]" ) img = driver.find_element_by_xpath("//img[@alt='div2-img2']/parent::div" ) img = driver.find_element_by_xpath("//div[@id='div1']/child::img" ) //img[@alt='div2-img2' ]/ancestor::div //div[@name='div2' ]/descendant::img //div[@id ='div1' ]/following::img //a[@href='http://www.sogou.com' ]/following-sibling::input //img[@alt='div2-img2' ]/preceding::div //input [@value='查询' ]/preceding-sibling::a[1 ] sogou_a=driver.find_element_by_xpath('//a[text()="搜狗搜索"]' ) sogou_a=driver.find_element_by_xpath('//a[.="搜狗搜索"]' ) baidu_a=driver.find_element_by_xpath('//a[contains(.,"百度")]' ) baidu_a=driver.find_element_by_xpath('//a[contains(text(),' 百度')]' ) div=driver.find_element_by_xpath('//a[contains(text(),"百度")]/preceding::div' ) div=driver.find_element_by_xpath('//a[contains(. , "百度")]/..' )
执行JavaScript 1 2 3 4 from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore' ) browser.execute_script('alert("xxoo")' )