Selenium webdriver - Unable to locate element on modal dialog - java

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

Related

Close window button clicked but window is not closed

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.

Making a hidden element clickable in selenium java

I have the following element:
<input type="hidden" data-dojo-attach-point="vn" value="adrija" aria-
hidden="true">
The above element is an element of dropdown and is hidden. The code that I have written is:
private WebElement adrija = Driver.driver.findElement(By.xpath("//input[#value='adrija' and #data-dojo-attach-point='vn']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", adrija);
It says it's not able to find the element.
Please help. Thanks. :)
Use css selector
driver.findElement(By.cssSelector("input[type='hidden']"))
Or xpath
driver.findElement(By.xpath("//input[#type='hidden']"))
Note : the field has type hidden. You cannot do visible interaction like sendkeys or click as it is invisible
Selenium cannot work on the hidden elements. First, you need the click on the button which opens the drop-down menu. Then you do whatever you want. :).
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath("XPATH"));
element.click();
WebElement childElement = driver.findElement(By.xpath("HidenElementXpath"));
childElement.click();
If the hidden element is in a frame, you need to switch to the frame!!!
Then try to find the element

Selenium - click does not trigger any action nor throws exception

I am using Selenium Webdriver and java and I need to click on this element:
here is the code:
driver.get(urlHp);
WebDriverWait wait = new WebDriverWait(driver, 10);
By btn = By.cssSelector("#_content-it_it_jcr_content_home-par1_promo_-par_o > div > li> a> div.btn-wrapper > button".trim());
wait.until(ExpectedConditions.visibilityOfElementLocated(btn));
driver.findElement(btn).click();
I can get the element and do not throw any exception but the click does not work.
Please note that the element is below the viewport.
How can I fix it?
Try Javascript executor:
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", driver.findElement(btn));
May be you can try with Actions class as given below.
driver.get(urlHp);
WebDriverWait wait = new WebDriverWait(driver, 10);
By btn = By.cssSelector("#_content-it_it_jcr_content_home-par1_promo_-par_o > div > li> a> div.btn-wrapper > button".trim());
wait.until(ExpectedConditions.visibilityOfElementLocated(btn));
Actions actions = new Actions(driver);
WebElement btnElement=driver.FindElement(btn);
actions.MoveToElement(btnElement).Click(btnElement).Perform();
Selenium can only click visible elements. By this I mean, that it should work as a user would use your app.
What Ranjith's showed you:
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].click()", driver.findElement(btn));
This is clicking the button with actual javascript code. For a quickfix this is fine. But remember that this implementation will also click the element if it's size is 1px by 1px. Test will pass, but your app is not usable.
I would rather recommend for selenium to use scrollIntoView method.
https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
This way you are closer to the actual user flow.

How to enable a disabled button in Java for Selenium webdriver?

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

Mouse Hover Action-Unable to click hidden Link

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

Categories

Resources