• selenium webdriver XPath的定位方法练习 !


    html  代码:

    <html>
    <body>
    
    <div id="div1">
    <input name="divl1input"></input>
    <a href="http://www.sogou.com/">搜狗搜索</a>
    <img alt="div1-img1 "src="http://www.sogou.com/images/logo/new/sogou.png"
    href = "http://www.sogou.com">搜狗图片</img>
    <input type ="button" value="查询"></input>
    </div>
    <br></br>
    <div name="div2">
    <input name="div2input"></input>
    <a  href = "http://www.baidu.com">百度搜索</a>
    <img alt="div2-img2" src="http://www.baidu.com/img/bdlogo.png" 
    href= "http://www.baidu.com">百度图片</img>
    <input type="button" value="查询"></input>
    </div>
    
    <br></br>
    <br></br>
    <br></br>
    <br></br>
    <a href="http://www.sogou.com"> 搜狗搜索</a><br></br>
    <a href="http://www.baidu.com"> 百度搜索</a>
    
    
    <br></br>
    <br></br>
    <br></br>
    <br></br>
    
    <table width="400" border="1" id= "table">
    <tr>
    <td align="left" >消费项目。。。。</td>
    <td align="right"> one  month </td>
    <td align="right">two  month</td>
    </tr>
    
    
    <tr>
    <td align="left" >衣服</td>
    <td align="right"> 1000 元 </td>
    <td align="right">2000 元</td>
    </tr>
    
    <tr>
    <td align="left" >化妆品</td>
    <td align="right"> 10 元 </td>
    <td align="right">20 元</td>
    </tr>
    
    
    <tr>
    <td align="left" >零食</td>
    <td align="right"> 120 元   </td>
    <td align="right">100 元  </td>
    </tr>
    
    
    <tr>
    <td align="left" >总计</td>
    <td align="right">88880 元 </td>
    <td align="right">10000  元</td>
    </tr>
    
    
    
    
    </table>
    </body>
    </html>
    

    java 代码:

    package page_elements_positioning;
    
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class By_tar_name {
    
        public static void main(String[] args) {
            String url ="file:///C:/Users/ty/Desktop/selenium_test.html";
            System.setProperty("webdriver.firefox.bin", "D:\softerWare\firefox\firefox\firefox.exe");
            WebDriver driver ;
            driver = new FirefoxDriver();
            driver.get(url);
            driver.findElement(By.tagName("a"));
            
            WebElement link=driver.findElement(By.tagName("a"));
            List<WebElement> links =driver.findElements(By.tagName("a"));
            
            //link.click();
            System.out.println(links.get(0)+"--------"+links.get(1));
            links =driver.findElements(By.xpath("//input[2]"));
            System.out.println("2----------"+links.size());
            
    // 1..绝对路径 ---查找 ;
        links.addAll(driver.findElements(By.xpath("/html/bodydiv/input[@value='查询']")));
        System.out.println(links.size());
    //2.--使用相对路径 ;        
            driver.findElement(By.xpath("//input[2]")).click();
    //3.. 使用页面的属性值 来定位原素实例; 
            WebElement img=driver.findElement(By.xpath("//img[@alt='div1-img1']"));
            //定位第一张图片:
            //mg[@href='www.sogou.com']
            //定位第二个div中的input的值;
            //div[@name='div2']/input[@name='div2input'] ;
            //定义第一个div中的第一个链接 
            //div[@id='div1']/a[@herf='www.sogou.com']
            //定位页面的查询按钮;
            //input[@type='button']
            
            
            
            
    
        }
    }
  • 相关阅读:
    转载《XAMPP安装和使用教程》(转)
    EA经典教程(转)
    something to note
    XAMPP安装说明及操作指南 (转)
    Use vCard Objects in RDF/XML (转)
    word2007的相关操作如删除标记区等 及firefox 修改颜色
    jsp和servlet重定向
    HowToMakeCustomSearch
    关于树型dropdownlist的绑定
    在ASP.NET中显示进度条
  • 原文地址:https://www.cnblogs.com/linbo3168/p/6090666.html
Copyright © 2020-2023  润新知