以下代码在Xpath中生成错误。 xpath用于动态元素,我使用start-with。在运行代码之前,我没有收到任何错误,但是在运行代码之后,eclipse产生了错误。请帮忙:
package com.TSOne.tcone;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class YahooTextSearch {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/owner/desktop/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
driver.findElement(By.id("uh-search-box")).sendKeys("selen");
List<WebElement> list=driver.findElements(By.xpath("//*[starts-with(@id,'yui_3_18_0_3_1528696’)]"));
System.out.println(list.size());
}
}
最佳答案
至少要改变
//*[starts-with(@id,'yui_3_18_0_3_1528696’)]
^
至
//*[starts-with(@id,'yui_3_18_0_3_1528696')]
^
(将标记的卷曲单引号替换为直线单引号。)
如果您还有其他问题,请进行详细跟进。请注意,通常在XPath中,尤其是在Selenium中,
//*
是一项昂贵的操作[感谢有用的评论,@cruisepandey] –如果可能,指定元素名称将避免潜在的性能问题。