如何在xpath中传递变量字符串来标识Selenium和Java中的元素?
代码试用:
System.out.println("Name:n");
String name = sc.nextLine();
System.out.println("Enter the no. of messages:n");
int count = sc.nextInt();
System.out.println("Enter what you want to send:n");
String mess = sc.next();
System.out.println("Enter anything after you have scanned the QR");
String a = sc.next();
driver.findElement(By.xpath("//span[@title='Rinmay AEI']")).click();
我希望我的用户输入name
字符串被放置,而不是"Rinmay AEI">
与其对title属性的值进行硬编码,不如将其设为变量,然后您可以使用以下定位器策略之一:
-
cssSelector
:System.out.println("Name:n"); String name = sc.nextLine(); // other lines of code driver.findElement(By.cssSelector("span[title='" + name + "']")).click();
-
xpath
:System.out.println("Name:n"); String name = sc.nextLine(); // other lines of code driver.findElement(By.xpath("//span[@title='" + name + "']")).click();