0
<h3 _ngcontent-ydk-c51="" matline="" class="mat-line">Text1</h3>

<h3 _ngcontent-ydk-c51="" matline="" class="mat-line">Text2</h3>

There are many links with class ="mat-line", but i want to click on link which contains Text1, how to do it ?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

3 Answers3

0

To get an h3 class of mat-line with text Text1 try the following xpath.

//h3[contains(text(), "Text1") and @class="mat-line"]
Arundeep Chohan
  • 6,219
  • 4
  • 6
  • 22
0

To click on the element with text as Text1 you need to induce WebDriverWait for the elementToBeClickable() (Java) / element_to_be_clickable() (Python) and you can use either of the following Locator Strategies:

  • Using Java and xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[@class='mat-line' and text()='Text1']"))).click();
    
  • Using Python and XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[@class='mat-line' and contains(., 'Text1')]"))).click()
    
  • Note: Using Python client 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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

Found these two xpath working for Selenium with C#

"//h3[contains(text(),'Text1')]"

"//*[@class='mat-line'][.='Text1']"