I am trying to do a mouse hover function on the menu in Cricinfo website.
It's not throwing any error and also the intended operation is not done.
Can anyone suggest me where the problem is or a possible way to find a way to debug it.
File file = new File(".\\Config\\Driver\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.get("http://www.espncricinfo.com/ci/engine/series/index.html?view=current");
driver.manage().window().maximize();
Thread.sleep(7000);
Actions action = new Actions(driver);
WebElement eleFirstLevel = driver.findElement(By.xpath("//a [#href='/ci/engine/match/index.html?view=live']"));
action.moveToElement(eleFirstLevel).perform();
action.moveToElement(driver.findElement(By.xpath("//ul//li[3]"))).click().build().perform();
Thread.sleep(5000);
driver.quit();
Please check the Xpath:-
action.moveToElement(driver.findElement(By.xpath("//ul//li[3]"))).click().build().perform();
Check this code:
driver.get("http://www.espncricinfo.com/ci/engine/series/index.html?view=current");
driver.manage().window().maximize();
Thread.sleep(7000);
Actions action=new Actions(driver);
WebElement eleFirstLevel=driver.findElement(By.xpath("//a [#href='/ci/engine/match/index.html?view=live']"));
action.moveToElement(eleFirstLevel).perform();
action.moveToElement(driver.findElement(By.xpath(".//*[#id='nav_grp']/li[2]/div[2]/ul/li[3]"))).click().build().perform();
Thread.sleep(5000);
driver.close();
Related
I have tried all the methods including xpath but I am still not able to click on the Accept button on cookies popup on https://www.news.sky.com
Tried css selector, xpath, frame etc everything.
Here is my code:
public class Browser {
WebDriver driver;
public void browser_open() {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectPath+"\\Drivers\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
public void navigate() throws InterruptedException {
driver.get("http://news.sky.com");
//Thread.sleep(5000);
driver.switchTo().frame("sp_message_iframe_368417");
driver.findElement(By.xpath("/html/body/div/div[3]/div[3]/button[1]")).click();
}
}
Please can someone help me on this?
I have already gone through a lot of posts on this and other forums but couldn't find an solution.
Thanks
I would suggest the xpath of //button[#title='Accept'] for what it's worth.
It's possible the switch to doesn't work because the element doesn't exist yet in the frame.
driver.switchTo().frame("sp_message_iframe_368417");
WebDriverWait wait = new WebDriverWait(driver, 10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#title='Accept']")));
driver.findElement(By.xpath("//button[#title='Accept']")).click();
I am trying to learn Selenium from Youtube. I have written the simple code below on Frames. I want to click on linkText which is not visible but manually can scroll and click on it. I am trying with the below code but getting the error:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable
My code:
public class Frame_Test {
WebDriver driver;
#Test
public void test1() {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("org.openqa.selenium.safari")).click();
}
}
You can scroll down with screen height using this code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
Scrolling down for some number of pixels:
js.executeScript("scroll(0, 300);");
Scrolling up for some number of pixels:
js.executeScript("scroll(0, -300);");
Hope it helps you!
You can use the below method:
public static void scrollToElement(By elementToken, WebDriver driver){
WebElement element = driver.findElement(elementToken);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Or if you don't want to use the explicit method you can use the scroll code in your code:
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html";
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.switchTo().frame("packageListFrame");
WebElement element = driver.findElement(By.linkText("org.openqa.selenium.safari"));((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
driver.findElement(By.linkText("org.openqa.selenium.safari")).click();
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 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.
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());
}