Deriving Xpath for a text using Selenium and Python

I am not able to derive the xpath for the following Text "Payment complete. Your policy will be generated soon". I have tried using the default xpath by directly copying the same but it throws the following error during execution invalid selector:

The result of the xpath expression "//*[@id="__next"]/div/div[1]/div[2]/div[1]/div[1]/span/text()" is: [object Text]. It should be an element.

Snapshot:

enter image description here


The xpath expression...

//*[@id="__next"]/div/div1/div[2]/div1/div1/span/text( )

...returns a Text Node where as Selenium expects a WebElement.


To locate the element with text Payment complete. Your policy will be generated soon you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH 1:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()[contains(.,'Payment complete')]]")))
    
  • Using XPATH 2:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'Your policy will be')]")))
    
  • 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