Locate button id with selenium - java

I want to locate button ID using selenium web driver. I tried this code:
#Test
public void hello()
{
RemoteWebDriver driver = BrowserFactory.getDriver("chrome", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver.get("http://jenkins.mws.com:8080");
WebDriverWait waitPage = new WebDriverWait(driver, 20);
WebElement until = waitPage.until(ExpectedConditions.presenceOfElementLocated(By.id("ready")));
if (until.isDisplayed()){
System.out.println("button is displayed");
}
}
But I get Timed out after 20 seconds waiting for presence of element located by: By.tagName: button
Here is the code that I want to test:
http://pastebin.com/up29pSRQ
Hwo I can locate tag button?

<button> is inside an iFrame. Switch to the iFrame first driver.switchTo().frame("iwg-game-full"); and then try.

Related

How to wait for a loader inside a button to complete loading?

I need to wait for a specific loader to complete loading once the button has been pressed, please take a look at the following image below:
As you can see from the image above, once the button has been pressed the ajax loader appears inside the button.
I have created the following selector to locate the button:
//form[contains(#id, 'messageform')]//button/span
Currently accepting the request (Clicking on the button) fails my test case as the script continues to the next test steps without waiting for the loader to complete.
I have tried the following and more, with no luck:
Injecting JS to wait for the page to fully load.
ExpectedCondition<Boolean> expectation = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
ExpectedConditions.invisibilityOf(element)
WebDriver driver = getDriver();
WebDriverWait exists = new WebDriverWait(driver, timer);
exists.until(ExpectedConditions.refreshed(
ExpectedConditions.invisibilityOf(element)));
Any ideas?
You should use .stalenessOf() to wait until an element is no longer attached to the DOM.
Something like this (tweak to your case):
WebElement somePageElement = driver.findElement(By.id("someId"));
WebDriverWait wait = new WebDriverWait(webDriver, 10);
// do something that changes state of somePageElement
wait.until(ExpectedConditions.stalenessOf(somePageElement));
And the good thing is you don't have to handle any exceptions.
Alternatively, you can also create a method and handle exceptions like so:
public static void WaitForCommission (WebDriver driver) throws Exception {
for (int second = 0; second++) {
if (second >= 30) fail("timeout");
try {
if (IsElementActive(By.id("someElementID"), driver))
break;
} catch (Exception e) {}
Thread.sleep(1000);
}
}
private static boolean IsElementActive(By id, WebDriver driver) {
WebElement we = driver.findElement(id);
if(we.isEnabled())
return true;
return false;
}

unable to click "ok" on javascript alert after clicking logout in Microsoft edge selenium with java

try {
driver.findElement(By.xpath("//a[#href='javascript:confirmLogout()']")).click();
//unable to execute any code after this
Thread.sleep(3000);
driver.switchTo().alert().accept();
System.out.println("hello"); // unable to print
//execution stops after logout click event
I suppose javascript fxn in #href is not getting executed, so you can invoke it explicitly using JavascriptExecutor as following, see if it works:
try {
driver.findElement(By.xpath("//a[#href='javascript:confirmLogout()']")).click();
JavascriptExecutor jse = (JavascriptExecutor ) driver;
jse.executeScript("confirmLogout();");
Thread.sleep(3000);
driver.switchTo().alert().accept();
System.out.println("hello");
}

Unable to perform mouse hover action which clicking on a link

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.

Selenium : Automating LinkedIn - Profile Icon

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.

Generating Java cssSelector to click span element in Selenium

I'm calling IE driver to launch this webpage (Business Objects). I'm able to login with the credentials. I need to click on a element on the next page. Need help writing the java to read this element and click.
<span style="white-space:nowrap;" class="iconText" id="IconImg_Txt_btnListing">Document List</span>
This is what I got so far using firebug-firepath.
driver.switchTo().defaultContent();
pickObj = driver.findElement(By.cssSelector("#IconImg_Txt_btnListing"));
pickObj.click();
Update: Another attempt-
public class InitComp {
//private WebDriver driver;
#FindBy(how = How.CSS, using = "#IconImg_Txt_btnListing") private WebElement DocListBtn;
public void clickDocList() {
DocListBtn.click();
}
}
This class is called as-
InitComp init = new InitComp();
PageFactory.initElements(driver, init);
init.clickDocList();
This does not help either though. Throws me an exception - "ElementNotFound". This page happens to be the first page after login. Where am I going wrong?
Thanks.
Arya

Categories

Resources