Selenium testing on Frames - java

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();

Related

Not able to identify an element in Selenium Webdriver (cookies pop-up on skynews)

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();

Posting Status in Facebook using Selenium and Java

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");

cannot select from a dropdpwn menu on firefox

I am using Java and Selenium to write a test. I have a drop down menu from which I need to select something. This is my code:
Select s= new Select(driver.findElement(By.xpath("blabla")));
s.selectByVisibleText("theName");
it works on Chrome but on Firefox 47 I get this error:
org.openqa.selenium.ElementNotVisibleException:
Element is not currently visible and so may not be interacted with
I know how to handle selecting from drop down menu by other ways, but I need to use Select object.
Use fluent wait to wait element, chrome is faster:
public static void waitUntilElementIsVisible(WebElement element, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.MINUTES);
wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
System.out.println("Checking for the element!!");
if(element.isDisplayed() != true)
{
System.out.println("Target element is not visible");
}
return element;
}
};
wait.until(function);
}
Then you can call it:
WebElement el = driver.findElement(By.xpath("blabla"));
waitUntilElementIsVisible(el, driver);
You can use the WebDriverWait class to wait for the visibility of the element like this:
WebDriverWait wait = new WebDriverWait(driver, customTime);
WebDriver driver = new FirefoxDriver();
Select s = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("blabla")));
s.selectByVisibleText("theName");

Mouse hover not working in Selenium Webdriver

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();

How can i perform mouse move using Selenium WebDriver for Java and PageFactory

I have an link in HTML and I use Page Object pattern to write scripts with Selenium Webdriver. But my link is hidden and I can't perform MouseMove action when object is initialized with pageFactory.
Here is my class:
public class DashboardPage {
WebDriver driver;
#FindBy(xpath = Constants.admin)
public WebElement adminButton;
#FindBy(xpath = Constants.usersAndRoles)
public WebElement usersAndRolesButton;
#FindBy(xpath = Constants.users)
public WebElement usersButton;
public DashboardPage (WebDriver dr){
driver =dr;
}
public UsersPage goToUsersPage(){
adminButton.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Actions builder = new Actions(driver);
builder.moveToElement(usersAndRolesButton).build().perform();
//usersAndRolesButton.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
usersButton.click();
return PageFactory.initElements(driver, UsersPage.class);
}
Try using scroll to element and perform operation on it. Try following function.Pass your element to function,page will scrolled to that element and then perform any other operation-
public void scrollToElement(WebElement element){
int elementPosition = element.getLocation().getY();
elementPosition = elementPosition-200; // You can change 200 to any value if your page have sticky header
System.out.println(elementPosition);
String js = String.format("window.scroll(0, %s)", elementPosition);
((JavascriptExecutor)driver).executeScript(js);
}
//other operation
element.click(); //etc....
The following code worked for me.
#FindBy(xpath = Constants.usersLink)
public WebElement usersLink;
public UsersPage goToUsersPage() {
JavascriptExecutor usersPage = (JavascriptExecutor) driver;
usersPage.executeScript("arguments[0].click();", usersLink);
return PageFactory.initElements(driver, UsersPage.class);
}

Categories

Resources