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.
Related
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 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();
}
I am running the following method:
public void AssertPreviousSubmissionClick() throws InterruptedException{
EnterLoginCredentials();
PreviousSubmissionClick(); //After this action takes place a new Browser Tab Opens
Assert.assertTrue("Clicking the link didn't work!",oldLogo.isDisplayed());
Reporter.log("PASSED! User Successfully click the Previous Year(s) Submission Request and was taken into the Grant Process!");
}
I left a note at PreviousSubmissionClick() at which point a new browser Tab is open. At this point in the method I can no longer identify WebElements on the new browser tab that opens. Please suggest how I can find WebElements on the new browser tab.
You need to switch the focus to the new tab
String currentTab = driver.getWindowHandle();
for (String tab : driver.getWindowHandles()) {
if (!tab.equals(currentTab)) {
driver.switchTo().window(tab);
}
}
// do something on new tab
And to close the new tab and switch back
driver.close();
driver.switchTo().window(currentTab);
Try this ,it will work.
public void handle(){
WebDriver driver;
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-switch-windows/");
driver.findElement(By.xpath(".//*[#id='content']/p[4]/button")).click();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
System.out.println(driver.getTitle());
}
I am new to Selenium and trying to use Actions class to mouseover on the Profile icon available on linked in site to open the menu that appears on Mouseover of profile image.
Below is my code and when it reaches on to those lines the error comes : Unable to locate element..
This is happening with all the icons available on Linked on top bar ( messages / Flag icon etc.
Code :
public class LinkedIn {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() throws Exception {
String baseUrl = "http://www.linkedin.com/";
driver.get(baseUrl);
}
#Test
public void login() throws InterruptedException
{
WebElement login = driver.findElement(By.id("login-email"));
login.sendKeys("*****#gmail.com");
WebElement pwd = driver.findElement(By.id("login-password"));
pwd.sendKeys("*****");
WebElement in = driver.findElement(By.name("submit"));
in.click();
Thread.sleep(10000);
}
#Test
public void profile() {
// here it gives error to me : Unable to locate element
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='img-defer-id-1-25469']"));
action.moveToElement(profile).build().perform();
driver.quit();
}
}
It seems you have used incorrect xpath , Kindly check below example to mouse hover on Message button :
Thread.sleep(5000);
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='account-nav']/ul/li[1]"));
action.moveToElement(profile).build().perform();
Correct Xpaths are :
For Message Icon : "//*[#id='account-nav']/ul/li[1]"
For Connection Icon : //*[#id='dropdowntest']
Above code I just tested and working fine so will work for you.
I want to perform drag and drop in W3school web page using selenium. Code is working fine but output is not showing on the webpage.
link is :- http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
My code is :-
public String dragAndDrop(String object,String data){
APP_LOGS.debug("waiting for popup closer");
try{
driver.switchTo().frame("iframeResult");
WebElement element = driver.findElement(By.xpath(".//*[#id='drag1']"));
WebElement target = driver.findElement(By.xpath(".//*[#id='div1']"));
(new Actions(driver)).dragAndDrop(element, target).build().perform();
}catch(Exception e){
return Constants.KEYWORD_FAIL+" -- Unable to drag"+e.getMessage();
}
return Constants.KEYWORD_PASS;
}
We can also interact with keyboard / mouse events using Actions class and robot class in Selenium. I have used Robot class to resolve your issue.
The Robot class is present in java.awt package. You can check all the methods in the docs.
public static void Task1() throws AWTError, AWTException, InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.switchTo().frame("iframeResult");
WebElement element1 = driver.findElement(By.xpath(".//img[#id='drag1']"));
WebElement element2 = driver.findElement(By.xpath(".//*[#id='div1']"));
Actions action = new Actions(driver);
Point element3 = driver.findElement(By.xpath(".//*[#id='drag1']")).getLocation();
int i=element3.getX()+800;
int b=element3.getY()+250;
Robot robot = new Robot();
robot.mouseMove(i, b);
// Press left click of mouse
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.delay(4000);
robot.mouseMove(i+20, b-120);
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(10000);
driver.close();
}