0

I am trying to check the content of all link pages which are found on the required URL.

So I do the following:

1- find all links in the required URL

2- check the validation for links (href content)

3- navigate to each link

5- check our condition for each link

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

    }

although I tried to check this condition for one url and it worked but when navigate automatically to different links (urls) as the example above
then I have the following 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

har07
  • 88,338
  • 12
  • 84
  • 137
Hana90
  • 943
  • 5
  • 17
  • 37

3 Answers3

1

Your code fails probably because some href are not links and are loading an empty page. You need to filter the valid ones and remove the duplicates. To test all the links with 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"));
}
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • I got successfully get all links, but my problem while find specific element for a page link when navigating – Hana90 Mar 21 '16 at 06:49
  • please see this http://stackoverflow.com/questions/36128144/invalid-selector-matter-of-loading-page-or-not – Hana90 Mar 21 '16 at 10:15
0

According to the traceback, .///text()[normalize-space(.) = "condition"]/parent:: XPath expression is being used. It is actually invalid, you've probably meant:

.//text()[normalize-space(.) = "condition"]/parent::*
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • WebElement error= driver.findElement(By.xpath(".//text()[normalize-space(.) = \""+"condition"+"\"]/parent::*")); – Hana90 Mar 21 '16 at 06:46
  • org.openqa.selenium.NoSuchElementException: no such element *** Element info: {Using=xpath, value=.//text()[normalize-space(.) = "condition"]/parent::*} – Hana90 Mar 21 '16 at 06:47
  • please see this http://stackoverflow.com/questions/36128144/invalid-selector-matter-of-loading-page-or-not – Hana90 Mar 21 '16 at 10:14
0

It seems the answer above by Florent B. is pretty good.

Let me give some advises about coding style. I see that I you are not using full power of Selenide syntax. Your test can be simplified:

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"));
}
Andrei Solntsev
  • 480
  • 2
  • 8