2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The reason why your code is failing is surely due to the image name being changed as well => `Q0G2UVjVQ4_.png` – StyleZ May 07 '22 at 15:17

2 Answers2

0

You can use XPath Xpath = //img[@class = '_8idr img'] to click on that icon for your case sample code will look like this
Note :- use implicit Wait on driver instance or you can use explicit Wait for this particular element to wait till clickable.

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  
  • _8idr may change any second so that sounds like a bad idea. – WizKid May 08 '22 at 05:33
  • @wizKid yaa i agree with your concern **-8idr** sounds like a randomly generated class for the current scenario it is constant do depend on session changes or browser. – Himanshu Raj May 09 '22 at 02:34
0

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();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352