Home > Enterprise >  How to click on Close icon/element on the Facebook sign-up page using Selenium Java?
How to click on Close icon/element on the Facebook sign-up page using Selenium Java?

Time:05-08

I am new to Selenium and learning. On the Facebook sign-up page I want to click on the X as shown in the below screenshot, but I am unable to click it. It's an img element.

driver.findElement(By.xpath("//img[@src='https://static.xx.fbcdn.net/rsrc.php/v3/yC/r/Q0G2UVjVQ4_.png']")).click();

check the screenshot

CodePudding user response:

You can use XPath Xpath = //img[@class = '_8idr img'] to click on that icon for your case sample code will look like this

driver.get("https://www.facebook.com/");
driver.findElement(By.xpath("//a[contains(text(),'Create New Account')]")).click(); 
driver.findElement(By.xpath("//img[@class = '_8idr img']")).click(); // To click on the cross icon in sign Up page  

CodePudding user response:

The desired element is an <img> tag which is the preceding-sibling of a <div> tag which is the immediate ancestor of the <div> tag with text as Sign Up

cross


Solution

To click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[text()='Sign Up']//ancestor::div[1]//preceding-sibling::img[1]"))).click();
    
  • Related