【问题标题】:Selenium Webdriver: How to get the dynamic id using xpath?Selenium Webdriver:如何使用 xpath 获取动态 id?
【发布时间】:2016-12-16 02:22:30
【问题描述】:

我正在尝试获取以 'u_' 开头并以 '_5' 结尾并且在数据按字母顺序更改之间的元素的 id。所以,有时我会看到 'u_d_5' 有时会看到 'u_6_5' 。在那种情况下,如何获得动态ID? 我知道有一些方法,比如开始或结束,但它们对我根本不起作用。谁能给我一些建议? 代码sn-p:

WebElement cls=ff.findElement(By.xpath("//*[@id='u_6_5']"));

完整代码:

    String s=System.getProperty("user.dir");
    System.setProperty("webdriver.chrome.driver", s+"\\ChromeDriver\\chromedriver.exe");
    ChromeDriver ff=new ChromeDriver();
try{
    ff.manage().window().maximize();
    ReadfrmExcel rd=new ReadfrmExcel();//reading data
    String[][] readata=rd.excelRead();
    for(int i=1; i<readata.length; i++)
    {
    ff.get("http://www.facebook.com");
    Thread.sleep(1000);
    ff.findElementByXPath("//*[@id='email']").sendKeys(readata[i][0]);
    ff.findElementByXPath("//*[@id='pass']").sendKeys(readata[i][1]);
    ff.findElement(By.id("u_0_v")).click();
    System.out.println("Waiting for element to appear");
    Thread.sleep(3000);
    WebElement element=ff.findElement(By.id("userNavigationLabel"));
    element.click();
    System.out.println("Clicked drop down");
    //ff.findElementByXPath("//*[@id='userNavigationLabel']").click();
    System.out.println("sleeping 5 secs");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_')   and ends-with(@id, '_5']"));
    boolean vis=cls.isDisplayed();

    System.out.println(vis);
    Thread.sleep(8000);
    System.out.println("Sleeping 8 secs");
    cls.click();
    System.out.println("After clicking");

    System.out.println("Successfully logged out!!");
    //ff.close();
    ff.close();

    }
    }
    catch(Exception e){
        System.out.println(e);
    }

【问题讨论】:

    标签: java selenium xpath


    【解决方案1】:

    您可以使用 xpath 的 starts-withends-with 方法:

    WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_') and ends-with(@id, '_5')]"));
    

    如果这不起作用,您的浏览器可能只支持 xpath 1.0(有关详细信息,请参阅this answer)-> 那么您只能使用starts-with

    WebElement cls=ff.findElement(By.xpath("//*[starts-with(@id,'u_')]"));
    

    如果您确实需要检查 id 的结尾,那么您可以尝试以下 xpath:

    //*[starts-with(@id, 'u_') and substring(@id, string-length(@id) - 1) = '_5']
    

    【讨论】:

    • 感谢@drkthng..尝试您的解决方案..很快就会更新
    • 虽然没有成功 :-( ..throws me no such element error。我已经将我的问题更新为完整代码。请看看,让我知道我错过了什么!
    • 当您尝试查找该元素时,您确定该元素已经存在吗?你能发布那一刻的html吗?干杯!
    • 是的..它总是在那里,但它的 id 一直在变化。此外,我编写了一个从 Facebook 登录注销的脚本,因此对于“注销”,它的 ID 每次都会更改。希望你明白我的问题!谢谢!
    • @lssmesy 可能是您的浏览器不支持 xpath 2.0 的问题 -> 更新了我的答案
    【解决方案2】:

    我已尝试获取我必须使用的所有 ID。 首先获取定义了 id 属性的标签名称。

    java.util.List links = ff.findElements(By.tagName("span"));

    ArrayList arrList = new ArrayList();

    for (int i = 0;i

       if(links.get(i).getAttribute("id").startsWith("u_") && 
              links.get(i).getAttribute("id").endsWith("_5")){
                    arrList.add(links.get(i).getAttribute("id"));
                }
            }
    
      Now you will find all the ids which are starts with "u_" and ends with"_5".
    
     System.out.println("arrList elements : "+arrList);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多