Issue
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); }
*/
}
}
Solution
Thanks everyone for the answers. I was traversing wrongly to the child. Here is how I did it, I traversed to the child from the parent using CSS selector only and it worked. I am posting the code so it helps someone.
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:\\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.cssSelector("div[class='row footer-align footer-mobile-res']" + "> div:nth-child(1)"));
footerDriver.findElement(By.xpath("//div[1]/ul[1]"));
int links = footerDriver.findElements(By.tagName("a")).size();
for (int i = 0; i < links; i++) {
String linksToOpen = Keys.chord(Keys.CONTROL, Keys.ENTER); //
System.out.println(footerDriver.findElements(By.tagName("a")).get(i).getText()); // //
System.out.println(footerDriver.findElements(By.tagName("li")).get(i).getText()); //
footerDriver.findElements(By.tagName("a")).get(i).sendKeys(linksToOpen);
}
// Amazon.in facing problem here too
System.setProperty("webdriver.chrome.driver", "D:\\QA Workspace\\Browser Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.amazon.in/");
// div.makeFlex.appendBottom40.footerLinks
WebElement footerDriver = driver
.findElement(By.cssSelector("div[class='navFooterVerticalRow navAccessibility'] > div:nth-child(1)"));
int links = footerDriver.findElements(By.tagName("a")).size();
//
for (int i = 0; i < links; i++) {
String linksToOpen = Keys.chord(Keys.CONTROL, Keys.ENTER);
footerDriver.findElements(By.tagName("a")).get(i).sendKeys(linksToOpen);
}
}
}
Answered By - Shilpa Malhotra