Reputation: 31
Selenium WebDriver does not perform the required action although my Xpath is correct:
public class Openchrome{
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","C:\\Users\\DELL\\Downloads\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.amazon.in");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Sri Raghavendra Swamy");
driver.findElement(By.className("nav-input")).click();
driver.findElement(By.xpath("//span[contains(text(),'Sri Sadhguru Raghavendra Swamy Jeevitha Charitra')]")).click();
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@id='buybox-see-all-buying-choices-announce']")));
driver.findElement(By.xpath("//a[@id='buybox-see-all-buying-choices-announce']")).click();
}
}
Upvotes: 1
Views: 77
Reputation: 7563
Yes right, all your locators are correct. Your issue comes from the following line:
driver.findElement(By.xpath("//span[contains(text(),'Sri Sadhguru Raghavendra Swamy Jeevitha Charitra')]")).click();
After the above action, it will bring you to new tab. You need to switch first before to do something, so use .getWindowHandles()
after perform the above line, like this:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
//switch to second tab
driver.switchTo().window(tabs.get(1));
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@id='buybox-see-all-buying-choices-announce']")));
driver.findElement(By.xpath("//a[@id='buybox-see-all-buying-choices-announce']")).click();
If you want to go back to the first tab again, just switch again with: tabs.get(0)
.
Upvotes: 1