I am trying to test an E-Commerce website using Selenium webdriver. The problem in the test is that whenever I try to add stuff in the cart it just pops a news letter window which I tried handling using alert but I cannot.
Can someone please help me. I am attaching a screenshot below along with the code.
public class Ui {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='search_txt']")).sendKeys("K-6626-6U ");
Actions enter = new Actions(driver);
enter.moveToElement(driver.findElement(By.xpath(".//*[#id='site-search']/div/button"))).click().build().perform();
}
}
First thing is - Its not an alert it a window popup So you need to locate the close button and then click
Use below code for the same :
public class Ui
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//button[#class='close external-close']")))).click();
}
}
Here you have to use ExplicitWait until popup get visible and then have to perform click. If you won't use the wait then it will throw ElementNotVisibleException.
wait for some time and click on escape
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
//give own time
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions enter = new Actions(driver);
enter.sendKeys(Keys.ESCAPE).perform();
}
Related
How to Post Status in Facebook using Selenium and Java?I have tried below code which doesn't work.Able to login but getting error no such element while posting status.After login i am getting notification popup allow or block,how to handle this popup also? Below the code i am using for testing this.
public class NewTest {
private WebDriver driver;
#Test
public void testEasy() throws InterruptedException {
driver.get("https://www.facebook.com/");
Thread.sleep(5000);
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("pass")).sendKeys("password" + Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).click();
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).sendKeys("Hello World");
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).sendKeys(Keys.ENTER);
}
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\admin\\Desktop\\Test\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
Your XPath expression is not very correct, as far as I can see thte title of the relevant textarea looks like:
What's on your mind, user1984
so you need to amend your locator to use XPath contains() function like:
By.xpath("//textarea[contains(#title,\"What's on your mind\")]")
Using Thread.sleep is a performance anti-pattern, you should be using WebDriverWait instead. Example refactored code:
driver.get("https://www.facebook.com/");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("email");
wait.until(ExpectedConditions.elementToBeClickable(By.id("pass"))).sendKeys("password" + Keys.ENTER);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(#title,\"What's on your mind\")]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(#title,\"What's on your mind\")]"))).sendKeys("Hello World");
Trying to send data in table element (cell). Verified ID, Xpath, CssSelector and none them is wrong.Even, put timeout till page load. Even verified the iFrame (Already switchTo current iFrame).
No such element found error pops up every time. Not sure if I need to switchTo iFrame again after page load?
I tried all the possible ways but not pass through. I really appreciate any suggestions or new direction to think.
Thank you in advance.
Run my script for better insight where its failing.
public class SapDijon
{
WebDriver driver;
JavascriptExecutor jse;
public static void main(String[] args) throws Exception
{
SapDijon sapObj = new SapDijon();
sapObj.invokeBrowser();
sapObj.initializeSapDijon();
sapObj.ForecastME59();
}//End of Main
public void invokeBrowser()
{
System.setProperty("webdriver.chrome.driver", "U:\\Research Paper\\Selenium\\Drivers\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
}
public void initializeSapDijon() throws Exception
{
Thread.sleep(1200);
driver.get("http://dijon.cob.csuchico.edu:8041/sap/bc/gui/sap/its/webgui/?");
driver.findElement(By.id("sap-user")).sendKeys("H5");
Thread.sleep(1200);
driver.findElement(By.id("sap-password")).sendKeys("Onsjhjsa1087");
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).clear();
Thread.sleep(1200);
driver.findElement(By.id("sap-client")).sendKeys("485");
Thread.sleep(1200);
driver.findElement(By.id("LOGON_BUTTON")).click();
}
public void ForecastME59() throws InterruptedException
{
driver.switchTo().frame("ITSFRAME1");
Thread.sleep(800);
driver.findElement(By.xpath("//td[#id='tree#105#4#1']//span[#class='lsSTStatusImg lsMirrorRtl lsSTStatusIcon urSTExpClo urCursorClickable']")).click();
Thread.sleep(800);
Actions action = new Actions(driver);
WebElement md61 = driver.findElement(By.xpath("//span[#id='tree#105#6#1#1#i']"));
action.doubleClick(md61).perform();
driver.findElement(By.id("M0:46:::4:2-imgSymb")).click();
driver.findElement(By.id("M0:46:::4:26")).sendKeys("HH-F");
driver.findElement(By.id("M0:50::btn[0]")).click();
Thread.sleep(6000);
driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).click();
driver.findElement(By.xpath("//span[#id='tbl5732[1,8]_c-r']/input[#id='tbl5732[1,8]_c']")).sendKeys("100");
}
}//End of Class
After " action.doubleClick(md61).perform()" switch back from frame to default content and again switch to the available iframe.
To move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();
The issue persists because of a new window opened and the script was unable to find the element. The solution is to switch to a newly opened window to find element locator.
ArrayList tabs = new ArrayList (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
I am using www.flipkart.com,where I want to hover over "Appliances" and click on "Television".
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.flipkart.com");
driver.manage().window().maximize();
Thread.sleep(1000);
WebElement mainMenu = driver.findElement(By.xpath("//a[#title='Appliances']"));
WebElement submenuxpath = driver.findElement(By.xpath("//li[#class='Wbt_B2'][2]//li[#class='_1KCOnI _1h5QLb']//a[#title='Televisions']"));
Actions builder = new Actions(driver);
builder.moveToElement(mainMenu).perform();
//builder.moveToElement(submenuxpath).click().perform();
//driver.click(submenuxpath);
Thread.sleep(1000);
driver.close();
}
It is able to hover over on "Appliances". But I am getting error in driver.click(submenupath) or builder.moveToElement(submenuxpath).click().perform(). Where I am going wrong?
driver.click(submenupath) error: The method click(WebElement) is undefined for the type WebDriver. Quick fix : add cast to driver. Even if I am doing add cast,it is not working then. For builder.moveToElement(submenuxpath).click().perform(),no error is coming,but it is not clicking also.
Using AWT Robot with Selenium WebDriver + Java, I want to switch browser to full screen mode(that we achieve manually using hitting F11 key). Hence, I wrote following code:
#Test
public void f() throws AWTException, InterruptedException {
Robot robot = new Robot();
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
Thread.sleep(2000);
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(5000);
driver.quit();
}
But browser doesn't switch to full screen. Can someone suggest, how can I achieve that?
Use below simple test code but selenium always like to choose second google suggestion result as the search text:
For example:
I input "Selenium", google will give suggestion list like below:
Selenium
Selenium WebDriver
Then webdriver will always pick up "Selenium WebDriver". But I used webdriver to sendKeys as "Selenium".
Is it a bug to webdriver?
public class HelloWorld {
private WebDriver driver;
#Before
public void setUp() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(caps);
driver.get("http://www.google.com");
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
//Thread.sleep(5000);
driver.findElement(By.name("btnK")).submit();
}
}
I'm pretty sure googles immediate results are bugging you here. Once you are halfway typing your query, Google will start showing you the results already and the "btnK" button will no longer be visible. Try this in stead:
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
inputField.sendKeys(Keys.ENTER);
}