Home > OS >  Using contains() function with selenium on Python
Using contains() function with selenium on Python

Time:10-08

I'm trying to use contains function on selenium and trying to do a certain action if that specific text exists and click on it.

How to use contains function with driver.find.element(). So far I tried:

if driver.find_element(By.LINK_TEXT, contains("Hello World")) == True:
    driver.find_element(By.LINK_TEXT, "Hello World").click() 

and some variations of it.

Element:

<a href="/xyz" target="_blank" >"Hello World"</a>

CodePudding user response:

You can use contains():

driver.find_element(By.XPATH,".//*[contains(.,'Hello World')]")

or

driver.find_element(By.XPATH,".//*[contains(text(),'Hello World')]")

or

driver.find_element(By.XPATH,".//*[contains(@id,'Hello World')]")

Syntax:

//tagName[contains(@attribute_name,'attribute_value')] 

or

//*[contains(@attribute_name,'attribute_value')]

or

//*[contains(text(),'value')]

CodePudding user response:

There's the By.PARTIAL_LINK_TEXT selector option that can do substrings of link text.

  • Related