In the webpage I'm testing, there are 3 options in a drop down and on selecting each option, rows in a table gets populated. I'm just trying to locate the table value through xpath and I am getting an the exception. Sharing the code I tried and the page source snippets. Please, help me out!!
System.out.println("\n Unbranded Sample Packs tests: ");
Reporter.log(" Unbranded sample packs validation |");
WebElement sp=driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[3]/div[1]/div/div/a/button"));
if(sp.isDisplayed())
{
sp.sendKeys(Keys.ENTER);
Thread.sleep(3000);
String spurl=driver.getCurrentUrl();
if(spurl.contains("unbranded-sample-packs"))
{
//Business card sample kit:
WebElement w1=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
w1.click();
driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[1]/a/span")).click();
WebElement p=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));
if(p.isDisplayed())
{
if(p.getText().equals("£8.00"))
{
System.out.println("\n Business card sample kit displays the correct page");
}
else
{
System.err.println("\n Business card sample kit option displays the wrong page !");
}
}
else
{
System.err.println("\n Prices are not displayed");
}
// Reseller Catalogues:
WebElement selectorElement = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
try
{
WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
sE.click();
}
catch (NoSuchElementException e)
{
selectorElement.click();
WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
sE.click();
}
try
{
WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));
elementOfInterest.click();
System.out.println("\n Reseller catalogues option is clicked");
}
catch (NoSuchElementException e)
{
selectorElement.click();
WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));
elementOfInterest.click();
System.out.println("\n Reseller catalogues option is clicked");
}
// Select dropdown = new Select(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"))); dropdown.selectByVisibleText("Reseller Catalogues");
/*
*
* JavascriptExecutor executor= (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('ID').style.display='block';");
Select select = new Select(driver.findElement(By.id("ID")));
select.selectByVisibleText("value");
Thread.sleep(6000);
*/
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).isDisplayed())
{
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).getText().equals("£10.00"))
{
System.out.println("\n Reseller Catalogues displays the correct page");
}
else
{
System.err.println("\n Reseller Catalogues displays the wrong page !");
}
}
else
{
System.err.println("\n Prices are not displayed");
}
//Unbranded sample pack option is selected
WebElement selectorElement1 = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));
try
{
WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
sE.click();
}
catch (NoSuchElementException e)
{
selectorElement1.click();
WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));
sE.click();
}
try
{
WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));
elementOfInterest.click();
System.out.println("\n Unbranded Sample pack option is clicked");
}
catch (NoSuchElementException e)
{
selectorElement1.click();
WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));
elementOfInterest.click();
System.out.println("\n Unbranded Sample pack option is clicked");
}
new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
WebElement p3=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));
if(p3.isDisplayed())
{
new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));
WebElement p4=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));
if(p4.getText().equals("£12.00"))
{
System.out.println("\n Unbranded Sample Pack displays the correct page");
}
else
{
System.err.println("\n Unbranded sample pack displays the wrong page !");
}
}
else
{
System.err.println("\n Prices are not displayed");
}
System.out.println("\n 'Finished size' dropdowns validated \n");
Reporter.log(" 'Finished size' dropdowns Validation done |");
}
else
{
System.err.println("\n Sample Pack button links to a wrong path");
}
}
else
{
System.err.println("\n Sample packs button is not available");
}
System.out.println("\n Unbranded Sample packs validated \n");
Reporter.log(" Sample packs Validation done |");
}
Page Source: