java - Selenium 化物-无效选择器 : Unable to locate an element with the xpath expression

标签 java selenium xpath selenium-chromedriver selenide

我正在尝试检查在所需 URL 上找到的所有链接页面的内容。

所以我执行以下操作:

1-查找所需URL中的所有链接

2-检查链接的验证(href 内容)

3-导航到每个链接

5-检查每个链接的状况

    String[] links = null;
    int linksCount = 0;
    System.setProperty("webdriver.chrome.driver", "Resources/chromedriver.exe");
    WebDriverRunner.setWebDriver(new ChromeDriver());   
    driver= WebDriverRunner.getWebDriver();


    open("http://vanilla.sa/");
    List<WebElement> linksize = WebDriverRunner.getWebDriver().findElements(By.tagName("a")); 
    linksCount = linksize.size();
    links= new String[linksCount];
    out.println("List of links Available: ");  

    for(int i=0;i<linksCount;i++)
    {
        links[i] = linksize.get(i).getAttribute("href");

    } 
    // navigate to each Link on the webpage
    for(int i=0;i<linksCount;i++)
    {

        System.out.println(links[i]);
        driver.navigate().to(links[i]);
        Selenide.sleep(7);
        WebElement error = $(Selectors.byText("condition")); 
        $(error).shouldNotBe(visible)
            .shouldNotBe(text("condition"));

    }

虽然我尝试检查一个网址的这一条件并且它有效,但是当自动导航到不同的链接(网址)时,如上面的示例
那么我有以下异常(exception):

http://vanilla.sa/%D8%AD%D9%82%D8%A7%D8%A6%D8%A8/%D8%AD%D9%82%D8%A7%D8%A6%D8%A8-%D9%83%D8%A8%D9%8A%D8%B1%D8%A9 org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression .//*/text()[normalize-space(.) = "condition"]/parent::* because of the following error: TypeError: Failed to execute 'createNSResolver' on 'Document': parameter 1 is not of type 'Node'. (Session info: chrome=49.0.2623.87) (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.2 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 74 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html Build info: version: '2.50.1', revision: 'd7fc91b', time: '2016-01-29 19:04:49' System info: host: 'Hana', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_80' *** Element info: {Using=xpath, value=.//*/text()[normalize-space(.) = "condition"]/parent::*} Session ID: 94c50df8d0862e2060230a819395224e Driver info: org.openqa.selenium.chrome.ChromeDriver

最佳答案

您的代码失败可能是因为某些 href 不是链接并且正在加载空页面。您需要过滤有效的并删除重复的。 使用 Java/Selenium/ChromeDriver 测试所有链接:

WebDriver driver = new ChromeDriver();
driver.get("http://vanilla.sa");

ArrayList links = (ArrayList)((JavascriptExecutor)driver).executeScript(
    "var arr = {}, l = document.links;" +
    "for(var i=0; i<l.length; i++) {" +
    "  var link = l[i].href;" +
    "  if(link.startsWith('http'))" +
    "    arr[link] = 0;" +
    "}" +
    "return Object.keys(arr);");

for (Object link : links) {
    driver.get(link.toString());
    WebElement element = driver.findElement(By.xpath("//body"));
}

关于java - Selenium 化物-无效选择器 : Unable to locate an element with the xpath expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36119469/

相关文章:

javascript - 在selenium python中仅选择带有标题标签的Web元素

java - 启动/停止线程 Java

java - Xpath 不适用于 Selenium

python - 如何通过xpath选择父组件的父组件

java - Java中如何引用Method对象?

java - 如何在 Java 中以编程方式启动和停止 Amazon EC2 实例

java - Hibernate 显示标签大列表

java - Android & Java 简单代码

css - 使用 CSS 选择器选择下拉值

xpath - 在SOAP UI中声明的Xpath表达式:如何处理节点值(例如Abc的作品)的撇号等特殊符号