I want to find elements using two partial link texts. I want to do this to separate instances where the first link text is the the same as another one.

For example if link text 1 for an element is "Publix Eggs, Large" and link text 2 is "12 ct", and link text 1 for another item is also "Publix Eggs, Large" but its link text 2 is "18ct" I want to be able to find only the first items element if I were to write something like below:

enter image description here

EXAMPLE HTML

link and separated link texts

what i'm trying to avoid grabbing

1

There are 1 best solutions below

2
On

You can do that with xPath.

//a[contains(.,'Publix Eggs, Large') or contains(.,'12ct')]

however, if you are just concerned about fetching the first element, then you can use

find_element

link = driver.find_element(By.PARTIAL_LINK_TEXT, "Publix Eggs, Large")
link.click()

Note that even though there are multiple matching nodes in HTML, it will always pick the first one.

Using find_elements

links = driver.find_elements(By.PARTIAL_LINK_TEXT, "Publix Eggs, Large")
links[0].click()