0
votes

I am converting the Selenium IDE code to Selenium webdriver. I am using Junit and I am very new to Junit and chromedriver.I am trying to click and close the (x) image in a modal window and tried the following line of code

driver.findElement(By.xpath("//img[contains(@src,'//baseUrl/Images/Icons/16x16/close.png')]"));

The element has img class, title and src.

Could somebody help to fix this line of code to close the modal window. Thanks

3
Please post the relevant HTML. What happens when you run that code?JeffC

3 Answers

0
votes

Simply using .click()

driver.findElement(By.xpath("//img[contains(@src,'//baseUrl/Images/Icons/16x16/close.png')]")).click();

Also, I am not sure about the syntax for JUnit. I have used selenium web driver with Python only so the syntax might be different. But the click() method is what you are really looking for.

0
votes

You might want to get the xpath of close button and use click() method on it.

driver.findElement(By.xpath("//img[contains(@src,'//baseUrl/Images/Icons/16x16/close.png')")).click();
0
votes

It sounds like it's a timing issue. Just add a wait so that the dialog has a chance to be displayed and then click it.

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#ModalDialog img.modalClose"))).click();