【问题标题】:Selenide- invalid selector: Unable to locate an element with the xpath expressionSelenide- 无效选择器:无法使用 xpath 表达式定位元素
【发布时间】:2016-03-20 20:55:05
【问题描述】:

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

所以我做了以下事情:

1- 查找所需网址中的所有链接

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"));

    }

虽然我尝试检查一个 url 的这种情况并且它有效,但是当自动导航到不同的链接 (url) 时,如上例所示
那么我有以下异常:

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:无效选择器:由于以下错误,无法找到具有 xpath 表达式.//*/text()[normalize-space(.) = "condition"]/parent::* 的元素: TypeError:无法在“文档”上执行“createNSResolver”:参数 1 不是“节点”类型。 (会话信息:chrome=49.0.2623.87) (驱动程序信息:chromedriver=2.9.248315,platform=Windows NT 6.2 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:74 毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/invalid_selector_exception.html 构建信息:版本:'2.50.1',修订:'d7fc91b',时间:'2016-01-29 19:04:49' 系统信息:主机:'Hana',os.name:'Windows 8',os.arch:'amd64',os.version:'6.2',java.version:'1.7.0_80' *** 元素信息:{Using=xpath, value=.//*/text()[normalize-space(.) = "condition"]/parent::*} 会话 ID:94c50df8d0862e2060230a819395224e 驱动信息:org.openqa.selenium.chrome.ChromeDriver

【问题讨论】:

    标签: java selenium xpath selenium-chromedriver selenide


    【解决方案1】:

    您的代码失败可能是因为某些 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"));
    }
    

    【讨论】:

    【解决方案2】:

    根据回溯,正在使用.///text()[normalize-space(.) = "condition"]/parent:: XPath 表达式。它实际上是无效的,您可能的意思是:

    .//text()[normalize-space(.) = "condition"]/parent::*
    

    【讨论】:

    • WebElement error= driver.findElement(By.xpath(".//text()[normalize-space(.) = \""+"condition"+"\"]/parent:: *"));
    • org.openqa.selenium.NoSuchElementException: 没有这样的元素 *** 元素信息: {Using=xpath, value=.//text()[normalize-space(.) = "condition"] /父::*}
    【解决方案3】:

    Florent B. 上面的答案似乎还不错。

    让我给一些关于编码风格的建议。我看到我没有使用 Selenide 语法的全部功能。您的测试可以简化:

    System.setProperty("webdriver.chrome.driver",  "Resources/chromedriver.exe");
    System.setProperty("selenide.browser", "chrome");
    
    open("http://vanilla.sa/");
    List<String> links = new ArrayList<>();
    out.println("List of links Available: ");  
    
    for (SelenideElement link : $$("a")) {
      links[i] = link.attr("href");
    } 
    
    // navigate to each Link on the webpage
    for (String link : links) {
        System.out.println(link);
        open(link);
        sleep(7000); // this is milliseconds!
        $(byText("condition"))
            .shouldNotBe(visible)
            .shouldNotHave(text("condition"));
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2018-09-29
      • 2015-05-08
      • 2021-04-05
      • 2023-03-22
      • 2020-05-01
      • 2020-03-01
      • 1970-01-01
      • 2019-12-25
      相关资源
      最近更新 更多