zoukankan      html  css  js  c++  java
  • (六2)八种定位方式为了查找元素

    1.XML

    可扩展标记语言标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言

    与HTML类似,但是他是为了传输和存储数据而非显示数据。

    2.XPath

    XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。

    XPth用来对 XML 文档中的元素和属性进行遍历,除了可以查找XML的节点,也可以查找HTML节点,因为这两个结构类似。

    当然现在前端和后端更多的交互都是用 Json 来传输 现在已经不用XML

    3.八种定位方式

    findElement

    findElements

    Selenium提供了8种定位方式。

    • id
    • name
    • class name
    • tag name
    • link text
    • partial link text
    • xpath
    • css selector

    这8种定位方式在Python selenium中所对应的方法为:

    • findElement(By.id())
    • findElement(By.name())
    • findElement(By.className())
    • findElement(By.tagName())
    • findElement(By.linkText())
    • findElement(By.partialLinkText())
    • findElement(By.xpath())
    • findElement(By.cssSelector())
    import org.openqa.selenium.By;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;
    public class getElementTest {
        WebDriver driver;
        @BeforeMethod
        public void openChrome(){
            System.setProperty("webdriver.chrome.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\chromedriver.exe");
            //实例化chromeDriver
            driver = new ChromeDriver();
        }
        /* eg:
        * html内容:
        * <html>
        * <head>
        * <body link="#0000cc">
        *<a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})">
        *<form id="form" class="fm" name="f" action="/s">
        *<span class="soutu-btn"></span>
        *<input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
        * */
        @Test
        public void findElementByid(){
            driver.get("http://www.baidu.com");
            //通过id定位:
            driver.findElement(By.id("kw"));
            //通过name定位:
            driver.findElement(By.name("wd"));
            //通过class name定位:
            driver.findElement(By.className("s_ipt"));
            //通过tag name定位:
            driver.findElement(By.tagName("input"));
            //通过tag name定位,tagname就是标签<后带的:
            driver.findElement(By.tagName("input"));
            //通过xpath定位,在火狐安装firePath、firebug之后,打开F12查看Xpath,xpath定位有N种写法,这里列几个常用写法:
            driver.findElement(By.xpath("//*[@id='kw']"));
            driver.findElement(By.xpath("//*[@name='wd']"));
            driver.findElement(By.xpath("//input[@class='s_ipt']"));
            driver.findElement(By.xpath("/html/body/form/span/input"));
            driver.findElement(By.xpath("//span[@class='soutu-btn']/input"));
            driver.findElement(By.xpath("//form[@id='form']/span/input"));
            driver.findElement(By.xpath("//input[@id='kw' and @name='wd']"));
            //通过css定位,css定位有N种写法,这里列几个常用写法:
            driver.findElement(By.cssSelector("#kw"));
            driver.findElement(By.cssSelector("[name=wd]"));
            driver.findElement(By.cssSelector(".s_ipt"));
            driver.findElement(By.cssSelector("html > body > form > span > input"));
            driver.findElement(By.cssSelector("span.soutu-btn> input#kw"));
            driver.findElement(By.cssSelector("form#form > span > input"));
            /*
             *
             * <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
             * <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
             */
            //通过link text定位: 注意只能是a标签,可以是完整标签也可以是部分文本标签
            driver.findElement(By.linkText("新闻"));
            driver.findElement(By.linkText("hao123"));
            driver.findElement(By.partialLinkText(""));
            driver.findElement(By.partialLinkText("hao"));
            driver.findElement(By.partialLinkText("123"));
        }
    
    
        @AfterMethod
        public void closeChrome(){
            driver.quit();
        }
    }

     优先id->name->Xpath

    实际运用:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;
    
    import java.util.List;
    
    public class getElementTest {
        WebDriver driver;
        @BeforeMethod
        public void openChrome(){
            System.setProperty("webdriver.chrome.driver","D:\\Program Files\\Java\\Webautomation\\drivers\\chromedriver.exe");
            //实例化chromeDriver
            driver = new ChromeDriver();
        }
        /* eg:
        * html内容:
        * <html>
        * <head>
        * <body link="#0000cc">
        *<a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})">
        *<form id="form" class="fm" name="f" action="/s">
        *<span class="soutu-btn"></span>
        *<input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
        * */
        @Test
        public void findElementByid(){
            driver.get("http://www.baidu.com");
            //当前的元素的类型是WebElement可以赋以下的值,如果a链接不能用
            WebElement e ;
            //通过id定位:
            driver.findElement(By.id("kw"));
            //通过name定位:
            driver.findElement(By.name("wd"));
            //通过class name定位:
            driver.findElement(By.className("s_ipt"));
            //通过tag name定位:
            driver.findElement(By.tagName("input"));
            //通过tag name定位,tagname就是标签<后带的:
            driver.findElement(By.tagName("input"));
            //通过xpath定位,在火狐安装firePath、firebug之后,打开F12查看Xpath,xpath定位有N种写法,这里列几个常用写法:
            driver.findElement(By.xpath("//*[@id='kw']"));
            driver.findElement(By.xpath("//*[@name='wd']"));
            driver.findElement(By.xpath("//input[@class='s_ipt']"));
            driver.findElement(By.xpath("/html/body/form/span/input"));
            driver.findElement(By.xpath("//span[@class='soutu-btn']/input"));
            driver.findElement(By.xpath("//form[@id='form']/span/input"));
            driver.findElement(By.xpath("//input[@id='kw' and @name='wd']"));
            //通过css定位,css定位有N种写法,这里列几个常用写法:
            driver.findElement(By.cssSelector("#kw"));
            driver.findElement(By.cssSelector("[name=wd]"));
            driver.findElement(By.cssSelector(".s_ipt"));
            driver.findElement(By.cssSelector("html > body > form > span > input"));
            driver.findElement(By.cssSelector("span.soutu-btn> input#kw"));
            driver.findElement(By.cssSelector("form#form > span > input"));
            /*
             *
             * <a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
             * <a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
             */
            //通过link text定位: 注意只能是a标签,可以是完整标签也可以是部分文本标签
            driver.findElement(By.linkText("新闻"));
            driver.findElement(By.linkText("hao123"));
            driver.findElement(By.partialLinkText(""));
            driver.findElement(By.partialLinkText("hao"));
            driver.findElement(By.partialLinkText("123"));
        }
        /*往第5个文本框内输入aaa*/
        @Test
        public void findElementsBy() {
            driver.get("http://www.baidu.com");
            List<WebElement> list = driver.findElements(By.tagName("input"));
            System.out.println(list.size());
            list.get(5).sendKeys("aaaaa");
        }
        /*输出一个列表中多个内容*/
        @Test
        public void  findElementsByXpath(){
            driver.get("http://www.baidu.com");
            //如果有多个元素,但是用的方法是findElement这边只返回第一个值
            WebElement e1 =driver.findElement(By.xpath("//*[@id=\"u1\"]/a"));
            String texte = e1.getText();
            System.out.println(texte);
            //用的是list返回多个元素
            List<WebElement> list =driver.findElements(By.xpath("//*[@id=\"u1\"]/a"));
            for (int i = 0; i < list.size() ; i++) {
                String text = list.get(i).getText();
                System.out.println(text);
            }
        }
    
        @AfterMethod
        public void closeChrome(){
            driver.quit();
        }
    }

     注意:Xpath是从数组是从1开始,list是从0开始

    如果linktext不是a标签 那这时候可以考虑用id 或 name或者是XPath,并且XPath建议用相对定位,因为这样研发在修改代码的时候,我们可能不需要修改代码

  • 相关阅读:
    买房的贷款时间是否是越长越好?https://www.zhihu.com/question/20842791
    asp.net cookie and session
    leelazero and google colab
    download file by python in google colab
    physical processor, core, logical processor
    通过powershell操作eventlog
    openxml in sql server
    get the page name from url
    How to Execute Page_Load() in Page's Base Class?
    Difference between HttpContext.Request and Request
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/9692206.html
Copyright © 2011-2022 走看看