两个输入框都具有完全相同的属性.如何使用唯一的xpath? [英] Both of the input box has exactly same attributes. How do I take unique xpath?

查看:234
本文介绍了两个输入框都具有完全相同的属性.如何使用唯一的xpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://www.phptravels.net 具有两个名称和属性相同的输入框(输入城市或机场).因此,我的xpath导致无此元素"异常.有没有解决的办法.请帮忙!以下是代码:

https://www.phptravels.net has two input boxes with same name and same attributes (Enter City Or Airport).Consequently, my xpath results in "no such element" exception. Is there any work around. Please help! Following are the code:

 package com.php.travels;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class Booking {

public static void main(String[] args) {
   System.setProperty("webdriver.chrome.driver", 
   "/Users/owner/desktop/chromedriver");

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("https://www.phptravels.net");
    driver.findElement(By.xpath("//span[text()='Flights']")).click();

    driver.findElement(By.xpath("//*[@id='select2- 
    drop']/div/input")).sendKeys("LAX");

   }

}

推荐答案

只有当您可以找到确切的定位器时,定位器才会出现问题,那么就不会有问题:

Problem is with the locators only if you can get the exact locator then there would not be the problem:

 package com.php.travels;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class Booking {

public static void main(String[] args) {
   System.setProperty("webdriver.chrome.driver", 
   "/Users/owner/desktop/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.get("https://www.phptravels.net");
        driver.findElement(By.xpath("//span[@class='hidden-xs' and contains(text(),'Flights')]")).click();
        driver.findElement(By.xpath("//span[@class='select2-chosen' and contains(text(),'Enter City Or Airport')]")).click();

        driver.findElement(By.xpath("//div[@class='select2-drop-mask']/following-sibling::div//input[contains(@class,'select2-input')]")).sendKeys("LAX");
   }

}

这篇关于两个输入框都具有完全相同的属性.如何使用唯一的xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆