Search code examples
python-3.xseleniumxpathcontainsnormalize-space

How to automate snackbar message which stays only for seconds using Python Selenium


I have to automate highlighted "P" tag text.

Snapshot of the element:

enter image description here

I am trying with below code. But its not identifying the element.

ele = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//p[text()="Connection Tested Successfully"]')))
if ele:
    print("Pass")
else:
    print("Fail")

Output:

enter image description here

Another snapshot:

enter image description here

Any solution on this??


Solution

  • The text within the <p> tag i.e. Connection Tested Successfully contains leading and trailing space characters which you need to consider while constructing the locator strategy and you can use either of the following locator strategies:

    • Using contains():

      ele = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//p[contains(., "Connection Tested Successfully")]')))
      
    • Using normalize-space():

      ele = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//p[normalize-space() = "Connection Tested Successfully"]')))