I am unable to click hidden link("WatchBanking") after using move-to-element.
WebElement lnkW2yB=dr.findElement(By.xpath("//a[#href='/personal/ways_to_bank/ways-to-bank-landing']"));
Actions act=new Actions(dr);
act.moveToElement(lnkW2yB).build().perform();
WebElement Span=dr.findElement(By.xpath("//span[contains(text(),'Bank with your Watch')]"));
WebDriverWait wait=new WebDriverWait(dr,20);
wait.until(ExpectedConditions.visibilityOf(Span));
act.moveToElement(Span).build().perform();
Thread.sleep(5000L);
WebElement lnk=dr.findElement(By.linkText("WatchBanking"));
wait.until(ExpectedConditions.visibilityOf(lnk));
act.moveToElement(lnk).click(lnk).build().perform();
It Moves to the span("Bank with your Watch") and shows link("WatchBanking").
But its not clicking on WatchBanking due to immediate disappearance.
Please give me any solution on this.
Selenium sometimes behaves like that only.I would go with JavascriptExecutor at times like this.I've repaced Selenium click by Javascript click and it worked perfectly for that site you've mentioned in comment.
Replace the lnk.click() by the following
WebElement lnk = dr.findElement(By.xpath("//a[text()='WatchBanking']"));
wait.until(ExpectedConditions.visibilityOf(lnk));
JavascriptExecutor js = (JavascriptExecutor) dr;
js.executeScript("arguments[0].click();", lnk);
Related
I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();
In selenium I successfully switch to an iFrame which contains a modal window:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='intercom-tour-frame']")))
In this iFrame is a close window button which is clicked "successfully" but the window does not close. By successfully I mean the button is found using the xpath and the action is completed without error in my code.
This is what I'm trying:
#FindBy(xpath = ("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))
private WebElement closeTestTourButton;
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
closeTestTourButton.click();
}
I've also tried:
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
waitForElementAndClick(closeTestTourButton, driver);
return this;
}
The test continues but fails as it tries to do an action but this is not possible due to the still open modal window.
Try clicking the button using the javascript, sometimes the events might not trigger with normal click.
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", closeTestTourButton);
return this;
}
I would suggest using the WebDriverWait rather delay in your script. Below is the implementation.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
Possibly you are switching and attempting to click() too early.
To click() on the close window button as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#name='intercom-tour-frame']")));
Induce WebDriverWait for the desired element to be clickable.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))).click();
But as you are using #FindBy presumably you are using PageFactory in PageObjectModel, so you won't be able to invoke WebDriverWait in conjunction with ExpectedConditions directly and you have to create a method. You can find a relevant detailed discussion in How to wait for invisibility of an element through PageFactory using Selenium and Java
Outro
Here you can find a relevant discussion on Ways to deal with #document under iframe
I don't like to answer my own questions but in this case this was the only solution that worked:
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
builder.sendKeys(Keys.ENTER).perform();
Granted, this is not the most elegant solution but after two days of trying it was the only one that worked.
I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.
I attached a screenshot from the code.
What can you recommend?
Thank you!
By.xpath(".//button[.='/"Submit/"'])
or
By.xpath(".//button[#class='btn btn-default'])
If it found but click doesnt work try that javascript from other comment
I presume you are able to identify the element.However unable to click on that.
Try use following options.
Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();
Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));
Note: If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.
driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.
You could try this:
JavascriptExecutor js= (JavascriptExecutor) driver;
WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
js.executeScript(“arguments[0].click()”, webElement);
Hope it helps.
Try the bellow xpath:
driver.findElement(By.xpath("//div[#class='modal-footer']//button[contains(#class,'btn-default')]")).click();
I want to disable the button before clicking on it, here is what I have:
<button type="submit" name="shippingAddress_save" value="Continue to Billing >" disabled="disabled">
you can do this in selenium USING JavascriptExecutor as below:
WebElement yourButton= driver.findElement(By.name("shippingAddress_save"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('disabled','disabled')",yourButton);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourButton));
yourButton.click();
You have to change the properties in the html side after doing click with selenium, but you can't do it with selenium itself, selenium only does what you tell it to do, but if you want change in your UI or your behavior you must have to write code for it.
you can check the below code :
WebElement element=driver.Find(By.xpath("your locator xpath")); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].removeAttribute('disabled','disabled')",element);
In my application I am not able to click on a partially visible check box, I even tried to do the same with javascript(scrool and click) and mouse hover actions, could any one find me a solution for the same?
You could try this:
WebElement g_element=g_driver.findElement(g_util.getObject("./ObjectRepository/"+objfile+".xml", objfile, office, "xpath"));
Webdriverwait wait = new Webdriverwait(driver,10)
wait.until(ExpectedConditions.elementToBeClickable(locator);
if(!elementChkBox.isSelected()) {
checkbox.click();
}