Below are the steps I want to automate on paytm via Selenium.
Steps:-
1.Launch Paytm.
2.Enter any keyword in Search box displayed at the top of paytm page.Eg."Mobile"
3.Press Enter to navigate to search result page.
Issue: Keyword written in Search box gets deleted automatically
My code:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class XPath {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\ProgramFiles\\Work\\ChromeDriver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://paytm.com");
//driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys("mobile");
driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys(Keys.ENTER);
}
}
@Grasshopper's analysis was in the right direction that you need to wait for the page to be loaded completely. I made a small test with your own code to retrieve the Page Title soon after invoking the url:
Code Block :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://paytm.com");
System.out.println(driver.getTitle());
Console Output :
Recharge - Online Mobile Recharge & Win 100% Cashback | Paytm.com
The initial page with this Page Title is an intermitent one when there are JavaScripts and Ajax Calls still active. So before you send the search String you need to induce WebDriverWait as follows :
Ideal Approach :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://paytm.com");
System.out.println(driver.getTitle());
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Paytm.com – Digital & Utility Payment, Entertainment, Travel, Payment Gateway & more Online !"));
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys("mobile");
driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys(Keys.ENTER);
Console Output :
Recharge - Online Mobile Recharge & Win 100% Cashback | Paytm.com
Paytm.com – Digital & Utility Payment, Entertainment, Travel, Payment Gateway & more Online !
Browser Snapshot :