标签:定位 findElements 一组 openqa selenium search import org
我们已经学习了8种定位方法, 那8种定位方法是针对单个元素定位的, WebDriver还提供了另外8种用于定位一组元素的方法。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.Keys; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; findElements(By.id()) findElements(By.name()) findElements(By.className()) findElements(By.tagName()) findElements(By.linkText()) findElements(By.partialLinkText()) findElements(By.xpath()) findElements(By.cssSelector())
定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词 findElement 后面多了一个 s 表示复数。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.List; public class ElementsDemo { public static void main(String[] args) throws InterruptedException { WebDriver driver = new ChromeDriver(); driver.get("https://www.baidu.com/"); WebElement search_text = driver.findElement(By.id("kw")); search_text.sendKeys("selenium"); search_text.submit(); Thread.sleep(2000); //匹配第一页搜索结果的标题, 循环打印 List<WebElement> search_result = driver.findElements(By.xpath("//div/div/h3")); //打印元素的个数 System.out.println(search_result.size()); // 循环打印搜索结果的标题 for(WebElement result : search_result){ System.out.println(result.getText()); } System.out.println("-------我是分割线---------"); //打印第n结果的标题 WebElement text = search_result.get(search_result.size() - 10); System.out.println(text.getText()); driver.quit(); } }
结果如下:
12 selenium__软件测试培训_免费领取试听课程 自动化测试工具selenium-新手入门宝典-实战性强 Selenium(WEB自动化工具)_百度百科 Selenium automates browsers. That's it!官方 selenium安装一款任何网站都能抓取的爬虫工具 selenium库的基本使用 - 简书 selenium中文网 | selenium安装、selenium使用、selenium中文... Python Selenium库的使用_凯耐的博客-CSDN博客_selenium selenium_百度翻译 selenium用法详解_天涯笨熊的博客-CSDN博客_selenium GitHub - SeleniumHQ/selenium: A browser automation framework... Selenium Projects -------我是分割线--------- Selenium(WEB自动化工具)_百度百科
标签:定位,findElements,一组,openqa,selenium,search,import,org 来源: https://www.cnblogs.com/peiminer/p/13564560.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。