Home > Mobile >  How to open links in footer with selenium
How to open links in footer with selenium

Time:10-06

I am new to Selenium and learning at the moment. I am trying to open footer links in selenium but it's not working on https://byjus.com/ or https://www.amazon.in I have already tried using Action class and also with Javascript by scrolling the element into view. Can someone please help me achieve it. Thanks in advance.

package WebApps;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class mmt {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\Nav QA Workspace\\Browser Drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://byjus.com/");
        WebElement footerDriver = driver.findElement(By.xpath("//div[@class='universal-footer']"));
        WebElement links = footerDriver.findElement(By.xpath("//div/div/div/div/div[1]/ul"));
        System.out.println(links.findElements(By.tagName("a")).size());

        for (int i = 0; i < 5; i  ) {
            String linksToOpen = Keys.chord(Keys.CONTROL, Keys.ENTER);
            System.out.println(links.findElements(By.tagName("a")).get(i).getText());
//          System.out.println(links.findElements(By.tagName("li")).get(i).getText());
//          links.findElements(By.tagName("a")).get(i).sendKeys(linksToOpen);
        }

        /*
         * Amazon.in facing problem here too
         * 
         * 
         * System.setProperty("webdriver.chrome.driver",
         * "D:\\Nav QA Workspace\\Browser Drivers\\chromedriver.exe"); WebDriver driver
         * = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(15,
         * TimeUnit.SECONDS); driver.get("https://www.amazon.in/"); //
         * div.makeFlex.appendBottom40.footerLinks WebElement footerDriver =
         * driver.findElement(By.cssSelector(
         * "div.navFooterVerticalColumn.navAccessibility"));
         * System.out.println(footerDriver.isDisplayed()); WebElement links =
         * footerDriver.findElement(By.xpath("//div/div[1]/ul"));
         * System.out.println(links.findElements(By.tagName("a")).size());
         * 
         * for(int i=0;i<links.findElements(By.tagName("a")).size();i  ) { String
         * linksToOpen = Keys.chord(Keys.CONTROL,Keys.ENTER);
         * System.out.println(links.findElements(By.tagName("a")).get(i).isEnabled());
         * System.out.println(links.findElements(By.tagName("li")).get(i).getText()); //
         * links.findElements(By.tagName("a")).get(i).sendKeys(linksToOpen); }
         */

    }
}

CodePudding user response:

I tested with a different locator type XPath. It completely works without any error. Would you try to click with XPath rather than CSS?

driver.get("https://www.amazon.in/");
WebElement footerDriver = driver.findElement(By.xpath("//div[@class='navFooterVerticalRow navAccessibility']"));
footerDriver.findElement(By.xpath(".//a[text()='About Us']")).click();

CodePudding user response:

It's more than enough to iterate this xpath"//div[@class='row footer-align footer-mobile-res']//div[1]//ul[1]//a"

I have done through WATIR, you could do the same in Java Selenium

require 'watir'
b=Watir::Browser.new

b.goto 'https://byjus.com/'
b.elements(xpath: "//div[@class='row footer-align footer-mobile-res']//div[1]//ul[1]//a").each do |a|
  p a.text
end

And If you wanted to click those links and wanted to open in another tab, then

b.elements(xpath: "//div[@class='row footer-align footer-mobile-res']//div[1]//ul[1]//a").each do |a|
  a.click(:control, :enter)
end

Output

"CBSE"
"ICSE"
"CAT"
"IAS"
"JEE"
"NEET"
"Commerce"
"JEE Main"
"NCERT"
"JEE Advanced"
"IAS Coaching"
"CBSE Sample Papers"
"CBSE Question Papers"
  • Related