How to click on facebook create new account button using Selenium Script

709 Views Asked by At

I am trying to run a selenium script for facebook create new account button. But am not able to click the create new account button via selenium script. Here is the code I am using below. I am getting error of:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

But is neither retrivable by class or ID. Please suggest!!!!

Code:

WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com/");
WebElement sign_in= driver.findElement(By.xpath("//a[@id='u_0_0_DX']"));
sign_in.click();
driver.close();

I am trying to run a selenium script for facebook new user signup but am not able to click the singup page button via selenium script.

2

There are 2 best solutions below

0
On

Check the code below if you want to click this button:

enter image description here

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.get("https://www.facebook.com/")
driver.maximize_window()
wait = WebDriverWait(driver,30)
# Click on Accept cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[@data-testid='cookie-policy-manage-dialog-accept-button'])[2]"))).click()
# Click on Create new account button
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@data-testid='open-registration-form-button']"))).click()
0
On

The id attribute values like u_0_0_DX, u_0_0_A5 are dynamically generated and is bound to change sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.

To click on Create new account you can use either of the following locator strategies:

  • Using partial_link_text:

    driver.find_element(By.PARTIAL_LINK_TEXT, "Create new account").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//a[contains(., 'Create new account')]").click()
    

Ideally to click on the clickable element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Create new account"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Create new account')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Facebook_SignUp